views:

30

answers:

1

Hello,

I have a char array "data" and a Int32 "dictIdFrame". I would like that dictIdFrame takes the value in ASCII (0-255) of data[i,...,i+3], I mean the four bytes become one single int32, where data[i] is the less significant and data[i+3] is the most significant in this int32. I really don't know how to do it...

var data = "asdfasdfasdfasdf";

for (var i=1; i<data.length; i++) 
{
    var dictIdFrame = // Here statement taking data[i],data[i+1],data[i+2],data[i+3]
}

If possible in one single instruction. THANKS FOR YOUR HELP!!

+1  A: 
// assume in group of 4:
for (var i = 0; i < data.length; i += 4) {
   var a = data.charCodeAt(i);
   var b = data.charCodeAt(i+1);
   var c = data.charCodeAt(i+2);
   var d = data.charCodeAt(i+3);
   var dictIdFrame = a | b << 8 | c << 16 | d << 24;
}

(Note, however, that a string in Javascript contains UTF-16 characters, not ASCII bytes. Therefore, it is possible that .charCodeAt will return a number ≥ 256.)

KennyTM