Hex string:
var str = [0x6A, 0xE8, 0x05, 0x01, 0x00, 0x01, 0xD9, 0xDC, 0x0A, 0x09];
byte = '';
for (var i=0; i < str.length; i++) {
byte += String.fromCharCode( parseInt(str[i], 16).toString(16) );
}
But receiver show:
6A C3A8 05 01 00 01 C399 C39C 0A 09
Any idea how to keep it 2 bytes? Or maybe my code wrong, esp. for nodejs?
*Updated the script. I'm kinda new with nodejs and I like to see what non-blocking events nodejs' offer because in "busy" day I have some missing data. Got the script working for python using binascii.(un)hexlify and PHP mbstring (un)pack (for web view).
Expecting:
6A E8 05 01 00 01 D9 DC 0A 09
Hex string:
var str = [0x6A, 0xE8, 0x05, 0x01, 0x00, 0x01, 0xD9, 0xDC, 0x0A, 0x09];
byte = '';
for (var i=0; i < str.length; i++) {
byte += String.fromCharCode( parseInt(str[i], 16).toString(16) );
}
But receiver show:
6A C3A8 05 01 00 01 C399 C39C 0A 09
Any idea how to keep it 2 bytes? Or maybe my code wrong, esp. for nodejs?
*Updated the script. I'm kinda new with nodejs and I like to see what non-blocking events nodejs' offer because in "busy" day I have some missing data. Got the script working for python using binascii.(un)hexlify and PHP mbstring (un)pack (for web view).
Expecting:
6A E8 05 01 00 01 D9 DC 0A 09
Share
Improve this question
edited Dec 20, 2012 at 5:25
lontongcorp
asked Dec 18, 2012 at 21:43
lontongcorplontongcorp
1381 silver badge8 bronze badges
3
- Why do you need: "String.fromCharCode" If you're already using: ".toString(16)" then you've already got a String . – Foggzie Commented Dec 18, 2012 at 22:00
-
1) Try typing
0x6a
in node interpreter and see what happens, note how it differs from"0x6a"
. 2) Argument forString.fromCharCode
is super strange. 3) Also, don't usefor in
with arrays. – Aleksei Zabrodskii Commented Dec 18, 2012 at 22:50 - This is for munication purpose. I need to translate the hexstring ining message, create the response message, send the response message back as hexstring too. So I need String.fromCharCode and quite confuse why it is called super strange. Thanks for the tips for {for in} ;) – lontongcorp Commented Dec 20, 2012 at 5:32
2 Answers
Reset to default 5Your code seems to be working for me if I decode it again. Careful though with for() loops over objects in Javacript. That also iterates over the properties of an object.
var str = [0x6A, 0xE8, 0x05, 0x01, 0x00, 0x01, 0xD9, 0xDC, 0x0A, 0x09];
byte = '';
for (var i=0; i < str.length; i++) {
byte += String.fromCharCode( parseInt(str[i], 16).toString(16) );
}
var hexarrayout = [];
for (var i=0; i<byte.length; i++) {
hexarrayout.push(byte.charCodeAt(i).toString(16));
}
alert(hexarrayout);
Example on http://jsfiddle/ycG4n/
Your problem looks very much like a utf8<=>iso-.../ascii/etc conversion problem when sending to your receiver, where some of your 1byte string chars are converted into 2byte chars. Due to the partial backwards patibility of UTF-8, some of your hex values would be kept 1byte.
Even though you're writing 0x6A
in your code, JavaScript automatically understands it as its value, 106. It is already a number, so parseInt()
won't do anything to it and can be removed.
You might just want to do this:
var str = [0x6A,0xE8...];
var byte = String.fromCharCode.apply(null,str);