Reading bytes from JavaScript string

Posted by Jan on Stack Overflow See other posts from Stack Overflow or by Jan
Published on 2009-08-06T17:46:38Z Indexed on 2010/12/24 20:54 UTC
Read the original article Hit count: 178

Filed under:

I have a string containing binary data in JS. Now I want to read, for example, an integer from it. So I get the first 4 characters, use charCodeAt, do some shifting etc. to get an integer.

Problem is that strings in JS are UTF-16 (instead of ASCII) and charCodeAt often returns values higher than 256.

The Mozilla reference states that "The first 128 Unicode code points are a direct match of the ASCII character encoding." (what about ASCII values > 128?)

How can I convert the result of charCodeAt to an ASCII value? Or is there a better way to convert a string of four characters to a 4 byte integer?

© Stack Overflow or respective owner

Related posts about JavaScript