DarkLeafyGreen
DarkLeafyGreen

Reputation: 70466

Hex string output different in browser and server javascript

I want to wrap a series of chars by hex strings, with this little script:

var resultBuffer = [];
var sourceBuffer = "004CS01".split('');

resultBuffer.push("\\x" + "02");

for (var i = 0; i < sourceBuffer.length; i++) {
    resultBuffer.push(sourceBuffer[i]);
}

resultBuffer.push("\\x" + "03");
resultBuffer.push("\\x" + "62");
console.log(resultBuffer);   

I am facing a problem where one and the same script creates different output. This is from browser: Fiddle http://jsfiddle.net/m5sUD/

["\x02", "0", "0", "4", "C", "S", "0", "1", "\x03", "\x62"] 

And this from nodejs server (see http://runnable.com/me/U1NzLLgIj8pXRQq4 ):

[ '\\x02', '0', '0', '4', 'C', 'S', '0', '1', '\\x03', '\\x62' ]

Why is it different? I need the output on the server to be just like in the browser.

Upvotes: 0

Views: 289

Answers (2)

Curry
Curry

Reputation: 895

resultBuffer values of browser and node.js are same. If you print each value in resultBuffer, node.js and browser print same value.

try below code

for (idx in resultBuffer) console.log(resultBuffer[idx]);

in my pc, node.js and browser print same below values.

\x02
0
0
4
C
S
0
1
\x03
\x62

it's a just matter of representation method of array values in node.js and browser. values are same.

Upvotes: 2

Amit Joki
Amit Joki

Reputation: 59272

I think it is due to fact that \ is not a special character in browser javascript, while it is a special character in node.js and hence it is escaped by another backslash \

Upvotes: 1

Related Questions