Created
April 18, 2011 03:41
-
-
Save dalmaer/924773 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var file = jDataView.createBuffer( | |
| 0x10, 0x01, 0x00, 0x00, // Int32 - 272 | |
| 0x90, 0xcf, 0x1b, 0x47, // Float32 - 39887.5625 | |
| 0, 0, 0, 0, 0, 0, 0, 0, // 8 blank bytes | |
| 0x4d, 0x44, 0x32, 0x30, // String - MD20 | |
| 0x61 // Char - a | |
| ); | |
| // Now we use the DataView as defined in the specification, the only thing that changes is the j before jDataView. | |
| var view = new jDataView(file); | |
| var version = view.getInt32(0); // 272 | |
| var float = view.getFloat32(4); // 39887.5625 | |
| The wrapper extends the specification to make the DataView easier to use. | |
| var view = new jDataView(file); | |
| // A position counter is managed. Remove the argument to read right after the last read. | |
| version = view.getInt32(); // 272 | |
| float = view.getFloat32(); // 39887.5625 | |
| // You can move around with tell() and seek() | |
| view.seek(view.tell() + 8); | |
| // Two helpers: getChar and getString will make your life easier | |
| var tag = view.getString(4); // MD20 | |
| var char = view.getChar(); // a | |
| // You can use a patched version of jQuery that supports ArrayBuffer for AJAX. | |
| $.get( | |
| 'data.bin', | |
| function (data) { | |
| var view = new jDataView(data); | |
| var tag = view.getString(4); // 'MD20' | |
| var version = view.getUint32(); // 732 | |
| }, | |
| 'binary' | |
| ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment