How to parse a binary file using Javascript and Ajax
I am trying to use JQuery to pull a binary file from a webserver, parse it in Javascript and display the contents. I can get the file ok and parse some of the file correctly. How ever I am running into trouble with one byte not coming out as expected.
I am parsing the file a byte at a time, it is correct until I get to the hex value B6 where I am getting FD instead of B6.
Function to read a byte
data.charCodeAt(0) & 0xff;
File As Hex: 02 00 00 00 55 4C 04 00 B6 00 00 00
The format I want to parse the file out into.
- short: 0002
- short: 0000
- string: UL
- short: 0004
- 开发者_开发知识库long: 0000B6
Any hints as to why the last value is incorrect?
A similar question was answered here. The short answer is that you can't easily handle binary data in javascript, and charCodeAt deals with Unicode characters so it's certainly not suitable for binary manipulation.
精彩评论