1

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 Answer 1

2
// 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.)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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