-
Notifications
You must be signed in to change notification settings - Fork 58
Standard types
Ingvar Stepanyan edited this page Aug 22, 2014
·
7 revisions
-
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.
-
float32(float) - classic 32-bitfloatused in languages like C. -
float64(double) - 64-bit float with double precision (doublein C), default for JavaScript number representation.
-
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 iflengthis not given. -
string0(@length, encoding = 'binary')- null-terminated string stored in given number of bytes; treated as dynamic null-terminated string iflengthis not given.
See info about encoding in jDataView documentation.
-
const(baseType, value, strict = false)- treats type as constant; if read value does not match expected and strict mode is enabled, callsstrict(readValue)if it is function or simply throwsTypeErrorif not. -
array(baseType, @length)- array of given type and length, reads/writes to the end of binary iflengthis 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").
-
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 iflengthis 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.
-
if(@condition, trueType, falseType)- conditional statement. -
if_not(@condition, falseType, trueType)- same but inverted. -
skip(@length)- simply skips given length on read/write.
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
}]
});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.