Skip to content
Ingvar Stepanyan edited this page Aug 22, 2014 · 7 revisions

Integers

  • uint8 (byte) / int8 - one-byte integer.
  • uint16 / int16 - word.
  • uint32 / int32 - dword (double-word).
  • uint64 / int64 - qword - please see warning about precision loss in jDataView documentation.

Floats

  • float32 (float) - classic 32-bit float used in languages like C.
  • float64 (double) - 64-bit float with double precision (double in C), default for JavaScript number representation.

Strings

  • char - one-byte binary character.
  • string(@length, encoding = 'binary') - string of given length in binary or 'utf8' encoding, reads/writes to the end of binary if length is not given.
  • string0(@length, encoding = 'binary') - null-terminated string stored in given number of bytes; treated as dynamic null-terminated string if length is not given.

See info about encoding in jDataView documentation.

Complex types

  • const(baseType, value, strict = false) - treats type as constant; if read value does not match expected and strict mode is enabled, calls strict(readValue) if it is function or simply throws TypeError if not.
  • array(baseType, @length) - array of given type and length, reads/writes to the end of binary if length is not given.
  • object(structure, proto = Object.prototype) - complex object of given structure (name => type), creates new context while processing inner properties; object may also contain functions instead of types for calculating some values during read/write for internal purposes.
  • extend(...object structures...) - extends one structure with others; merges data into one object when reading and passing entire object when writing.
  • enum(baseType, matches) - enumeration type with given key <=> value map (if value not found in the map, it's used "as-is").

Binary types

  • bitfield(length) - unsigned integer of given bit length (supports up to 32 bits, wraps around 2^32).
  • blob(@length) - byte array represented in most native type for current engine; reads/writes to the end of binary if length is not given.
  • binary(@length, typeSet = {}) - jBinary instance on part of original one with given length and optional custom typeset (useful for container formats); accepts also raw binary data when writing.

Control statements

  • if(@condition, trueType, falseType) - conditional statement.
  • if_not(@condition, falseType, trueType) - same but inverted.
  • skip(@length) - simply skips given length on read/write.

References

All the arguments marked with @(references) can be passed not only as direct values, but also as getter functions callback(context) or string property names inside current context chain.

Examples:

binary.read({
  count: 'uint32',
  bytes: ['array', 'uint8', 'count']
});
binary.read({
  recordSize: 'uint32',
  data: ['array', 'uint8', function (context) {
    return context.recordSize - 4; // total size except `size` field itself
  }]
});

Custom types

Usually, in-built types are not enough for handling complicated structures since you might want to create and re-use some own types built on top of standard ones. For such cases, you might want to have look at jBinary.Type.

Clone this wiki locally