1

I have an array of binary data that was converted using node js on server:

buffer.toString('hex');

And there are 2 integer added in the beginning of the buffer like so:

buffer.WriteInt32LE(index,0);
buffer.WriteInt32LE(id,0);

in the end i got a string like this:

var str = "1100000050000000fd2595200380024"

I need the parse this string on client, using windows scripting environment ( javascript + ActiveX)

How can i convert the first two values '11000000' and '50000000' from little endian string to integer, and the rest of the string to binary bytes represented by hex codes? In browser ArrayBuffer is avaliable, but the js executed from windows scripting environment.

2
  • node.js !== jscript. You should make it more clear what you are trying to do and where/with what. Commented May 25, 2017 at 16:37
  • @mscdex updated Commented May 25, 2017 at 16:46

1 Answer 1

1
var str = "1100000050000000fd25952003800245";
var int1 = parseInt(str.substring(0,7));
var int2 = parseInt(str.substring(8,15));
 alert(int1);
 alert(int2);

will get you the integers..

the rest of the str has 7 bytes... and a nibble... fd 25 95 20 03 80 02 4

assuming you are missing a nibble to make it 8 bytes...

var hex = [str.substring(16,str.length).length/2];

this will get you the byte array with 8 positions

var hex = [];
hex.push(str2.substring(0,2));
hex.push(str2.substring(2,4));
hex.push(str2.substring(4,6));
hex.push(str2.substring(6,8));
hex.push(str2.substring(8,10));
hex.push(str2.substring(10,12));
hex.push(str2.substring(12,14));
hex.push(str2.substring(14,16));

alert(hex);

just filled the array with the bytes in string, but you just need to convert them now :)

fullcode:

var str = "1100000050000000fd25952003800245";
var int1 = parseInt(str.substring(0,7));
var int2 = parseInt(str.substring(8,15));
var str2 = str.substring(16,str.length);
 alert(int1);
 alert(int2);
 var hex = [];

hex.push(str2.substring(0,2));
hex.push(str2.substring(2,4));
hex.push(str2.substring(4,6));
hex.push(str2.substring(6,8));
hex.push(str2.substring(8,10));
hex.push(str2.substring(10,12));
hex.push(str2.substring(12,14));
hex.push(str2.substring(14,16));
alert(hex);
Sign up to request clarification or add additional context in comments.

3 Comments

hi. But it give incorrect results, i tried: var hex1 = parseInt("7d000000"); outputs : 7
its diferent, you need to substring first. when the code reaches the character "d" , it assumes 7 as the intiger.
if you try the code just like i posted up there, it will work. or are you assuming first 16 characters can bring chars too?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.