Example

Ingvar Stepanyan edited this page Aug 27, 2013 · 2 revisions

First we need a file. Either you get it through XHR, File API or just using the createBuffer utility.

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" in the front of "DataView".

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(), seek() and skip()
view.skip(8);

// Helpers like getChar and getString will make your life easier
var tag = view.getString(4); // MD20
var char = view.getChar(); // a