Javascript hex string fromCharCode

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

Your 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.net/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 compatibility 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);