quote:
Originally posted by TheGuruSupremacy
Hi man, i need help....:I make this function:
function SetDataPatch(string){
var newstring =""
for (var i=0;i<string.length;i++){
newstring += "\x" + Hex(string.charCodeAt( i)) + "\x00"}
Debug.Trace(newstring)
return newstring}}
but plus found this error,this is in italian but i'll try to translate it in english:
Lo script è stato fermato(the script has been stopped)
Errore: Prevista cifra esadecimale.(Error: expect digit hex)
Linea: 43 Codice: -2146827265(line:43 Code: -2146827265)
i need to make a string like this "\xFF\x00\xFC" CAN SOMEONE HELP ME PLEASE??? Sorry for my english
The error is because the function
Hex doesn't exist in JScript. And if you've made your own
Hex function then note that "\x" must always be followed by a
2 digit hexadecimal number.
Thus "\xE" will not work either, it must be "\x0E".
PS: If you get such errors, try to split up the line which caused the error to see what part exactly triggered it...
Anyways, even if
Hex existed and it does always return a 2 digit number, your function wouldn't work in all cases either though, see below.
Also "\x00" will make a string with the ascii code character 0, not a string which will display \x00. The "\" character is an escape character, meaning it has special function if used inside a string (like defining a character by its hexadecimal code). To show the slash ("\") you must escape it with the escape character, thus "\\".
quote:
Originally posted by phalanxii
As far as I know, a function like this is completely useless because "a" === "\x61" (someone correct me on this one ).
Not always (it would only be useless in
option a, see below).
Seeing the function name ("patch") I suspect he wants to add null characters between the original characters (thus
option b, see below). In other words convert a string to its unicode counterpart. Or to put in in context with JScript: to convert a unicode string to double unicode notation...
eg: to convert the this byte data
FF 00 0A 00 0E 14
to
FF
00 00
00 0A
00 00
00 0E
00 14
00
where 00 are the added bytes
This could be usefull for patching and other related stuff...
--------------------------
However, TheGuruSupremacy,
Always specify exactly and in detail what you want and what the purpose of the function is when you request help. As you see above, the solution highly depends on what you exactly want with that function. Be very specific and give clear examples.
In situation likes this, it is even recommended to show the entire script so people can clearly see what the purpose it.
Since this function deals with unicode, ascii code, string literals, screen outputs, etc, it is very important to state clearly what is what.
eg: assuming the input string contains the two characters 0xFF and 0xFC (you didn't specify this either), when you say "i need to make a string like this "\xFF\x00\xFC" ", then what do you mean by 'string':
a) Do you mean a 2 character long string containing the unicode characters FF and FC
b) Do you mean a 4 character long string containing the characters FF, 00, FC and 00
c) Do you mean you want a 'string' which would literally display \xFF\x00\xFC\x00
To helpers: Try not to guess. If you aren't sure what is requested ask details and examples first
--------------------------
For now I assume that phalanxii guessed correctly and that you want to show literally the hexadecimal codes which make up a string, thus
option c:
quote:
Originally posted by phalanxii
However, if the function is to give "\\xFF\\x00\\xFC\\x00" (which will display as "\xFF\x00\xFC\x00" to the user), you can use this:code:
function SetDataPatch(string) {
var newstring = "";
for(var i = 0; i < string.length; i++) {
newstring += "\\x" + string.charCodeAt(i).toString(16) + "\\x00";
}
Debug.Trace(newstring);
return newstring;
}
But don't assume the characters in the string are always between ASCII 0 and 255, they can be anything from 0 to 65535. In other words they are always unicode.
Thus
charCodeAt(i) can return anything from 0 to 65535.
This means that you should never just add "\x00" to the string.
Thus,
toString(16) will not always return a 2 digit hexadecimal number, it might just be 1 digit or even up to 4 digits
(and it will always be lowercased). This also means that in the case of 1 digit the function will fail too (see reply to TheGuruSupremacy above).
The fixed function:
code:
function SetDataPatch(string) {
var newstring = "";
for (var i = 0; i < string.length; i++) {
// Get character and convert to hexadecimal
var charHexcode = string.charCodeAt(i).toString(16).toUpperCase();
// Since hexcode can be anything from 1 to 4 digits long
// we need to properly format it by adding 0's.
charHexcode = ("000" + charHexcode).substr(charHexcode.length-1);
// The least significant part (last 2 digits) is the
// first part to return in LE (little endian) format
newstring += "\\x" + charHexcode.substr(2);
// The most significant part (first 2 digits) is the
// last part to return in LE (little endian) format
newstring += "\\x" + charHexcode.substring(0, 2);
}
Debug.Trace(newstring);
return newstring;
}