I read a binary file using ajax from a web server. The HTTP response is of contentType: 'application/octet-stream' and contains a binary string that is just a string of bytes (not unicode), for example (in hex):
0x00 0x08 0x17 0xA1 0x01
Note: in C this would be represented as 5 bytes in memory:
char buf[5] = {0, 8, 23, 161, 1}
...but in Javascript that is a string which ASCII representation is something like " � " (I can't actually paste it properly as not all characters have a printable representation).
I now need to convert this into an array of characters or integers so that I can access the numerical value of each character in the string. However iterating through the example string using the charCodeAt() function returns:
[0] 0
[1] 8
[2] 23
[3] 65533
[4] 1
because charCodeAt() decodes unicode characters, and 0xA1 is not recognised as valid unicode character so a Replacement Character (65533) is used instead.
I would like to get the following:
[0] 0
[1] 8
[2] 23
[3] 161
[4] 1
How to achieve this?
Here is the code snippet:
$.ajax({
url: url,
type: "get",
success: function(data) { // data contains binary representation of 0x00 0x08 0x17 0xA1 0x01
var byteTab = []
for (var n = 0; n < data.length; ++n) {
byteTab.push(data.charCodeAt(n))
}
})