Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you access byte level information in JavaScript?

The generally accepted answer is that you can't. However there is mounting evidence that this is not true based on the existence of projects that read in types of data that are not basic HTML types. Some projects that do this are the JavaScript version of ProtoBuf and Smokescreen.

Smokescreen is a flash interpreter written in JS so if it is not possible to get at the bytes directly how are these projects working around this? The source to Smokescreen can be found here. I have looked it over but with JS not being my primary language right now the solution eludes me.

like image 407
QueueHammer Avatar asked Jun 03 '10 13:06

QueueHammer


1 Answers

They both look to be using a String (in this case the responseText of an XMLHttpRequest) directly as a collection of bytes.

data = ... // a binary string
bytes = [];
for ( i = 0; i < data.length; i++ )
{
  // This coverts the unicode character to a byte stripping
  // off anything past the first 8 bits
  bytes[i] = data.charCodeAt( i ) & 0xFF;
}
like image 76
jimr Avatar answered Oct 14 '22 14:10

jimr