Skip to content
Zade Viggers edited this page May 23, 2024 · 4 revisions

Note

This page has been updated for jDataView 3

First, we need a buffer, like so

// jDataView.from(...args) is a helper method that constructs a new jDataView instance
// with the provided spread arguments. It only works in little-endian.
let view = jDataView.from(
	0x00, 0x00, 0x01, 0x10, // Int32 - 272
	0x47, 0x1b, 0xcf, 0x90, // 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 change is the "j" in front of "DataView."

let version = view.getInt32(0); // 272
let float = view.getFloat32(4); // 39887.5625

The wrapper extends the specification to make the DataView easier to use.

// Create a new jDataView instance on the same buffer
view = view.slice(0);

// 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
const tag = view.getString(4); // MD20
const char = view.getChar(); // a
Clone this wiki locally