User Rating 0.0
Total Usage 0 times
Presets:
Use type names: boolean, int8, uint8, int16, uint16, int32, uint32, float, double, string, shortBuffer, buffer. Arrays: [{ ... }] or [[type]]
Is this tool helpful?

Your feedback helps us improve.

About

Binary serialization of JavaScript objects requires a rigid schema that maps each property to a fixed-width data type. Without a predefined scheme, you cannot reconstruct the original object from raw bytes - there are no delimiters, no field names, no type markers in the output buffer. This tool implements the exact serialization protocol used by the data-struct npm package: each primitive is written at its native byte width (1 byte for boolean/int8/uint8, 2 bytes for int16/uint16, 4 bytes for int32/uint32/float, 8 bytes for double), strings carry a 2-byte length prefix encoding the UTF-8 byte count (not character count), and arrays carry a 2-byte element count prefix. Nesting is resolved by recursive descent - the schema mirrors the object graph exactly.

Mismatched schemas produce silent data corruption, not exceptions. A uint16 field read as int32 shifts every subsequent offset by 2 bytes, cascading into garbage output. This tool validates type ranges before writing (e.g., clamping uint8 to 0 - 255) and flags overflows. It operates entirely in the browser using DataView and Uint8Array, producing output byte-identical to Node.js Buffer. Limitation: floating-point values follow IEEE 754 and may exhibit rounding artifacts at extreme precision.

buffer node.js binary serialization data-struct javascript typed array schema converter

Formulas

The total buffer size for a flat object with k fields is the sum of each field's byte footprint:

totalBytes = ki=1 size(fieldi)

For variable-length types, the byte footprint includes the header:

size(string) = 2 + byteLength(UTF8(value))
size(array) = 2 + n × size(elementSchema)

Where n is the number of elements in the array and byteLength returns the UTF-8 encoded byte count (not the JavaScript .length character count). Multi-byte Unicode characters (e.g., emoji) consume 3 - 4 bytes per character. The 2-byte header for strings and shortBuffers limits payload to 216 1 = 65535 bytes. The 4-byte header for buffer type allows up to 232 1 = 4294967295 bytes.

For nested objects, recursion applies: size(object) = ki=1 size(fieldi). No overhead bytes are added for object nesting itself - only the constituent fields contribute to size.

Reference Data

Type NameByte SizeValue RangeJS Read MethodJS Write MethodNotes
boolean10 - 1getUint8setUint80 = FALSE, any other = TRUE
int81−128 to 127getInt8setInt8Signed byte
uint810 to 255getUint8setUint8Unsigned byte
int162−32768 to 32767getInt16setInt16Signed short
uint1620 to 65535getUint16setUint16Unsigned short, used for array length headers
int324−2147483648 to 2147483647getInt32setInt32Signed integer
uint3240 to 4294967295getUint32setUint32Unsigned integer
float4±3.4 × 1038getFloat32setFloat32IEEE 754 single precision, ~7 significant digits
double8±1.8 × 10308getFloat64setFloat64IEEE 754 double precision, ~15 significant digits
string2 + nMax 65535 bytesTextDecoderTextEncoderHeader is uint16 byte length, UTF-8 encoded
shortBuffer2 + nMax 65535 bytesslicesetRaw binary with uint16 length header
buffer4 + nMax 4294967295 bytesslicesetRaw binary with uint32 length header
array2 + n × elementMax 65535 elementslooploopHeader is uint16 element count, not byte count

Frequently Asked Questions

This tool uses Big-Endian (network byte order) by default, matching the data-struct npm package's convention (which uses writeUInt16BE, writeUInt32BE, etc.). You can toggle to Little-Endian via the endianness switch. Node.js Buffer supports both - ensure your reading code matches the endianness used during writing.
JavaScript's .length counts UTF-16 code units, not bytes. UTF-8 encoding (used in the buffer) represents ASCII characters in 1 byte, Latin-1 extended in 2 bytes, CJK characters in 3 bytes, and emoji/supplementary characters in 4 bytes. A 5-character string containing emoji could produce 20+ bytes. The 2-byte uint16 header stores this byte count, not the character count. Maximum payload is 65535 bytes.
The tool clamps values to the valid range and displays a warning. For example, writing 300 to a uint8 field (range 0-255) clamps to 255. For signed types like int8 (range −128 to 127), overflow wraps modularly in raw DataView operations, but this tool catches it pre-write. Float and double follow IEEE 754 and silently lose precision beyond ~7 and ~15 significant digits respectively.
Nested arrays use recursive schema matching. A schema of [[DataTypes.uint8]] means "an array of arrays of uint8". The outer array writes a uint16 element count (number of inner arrays), then each inner array writes its own uint16 element count followed by its uint8 values. For a 7×8 grid of uint8, total bytes = 2 (outer count) + 7 × (2 + 8×1) = 2 + 70 = 72 bytes.
Yes. The binary format is plain sequential bytes - no magic numbers or format headers. You can read it with Node.js Buffer methods (readUInt32BE, readInt16BE, etc.) as long as you follow the same schema and offset sequence. The data-struct package simply automates this offset tracking. This tool's hex output can be loaded via Buffer.from(hexString, 'hex') in Node.js.
No. The buffer contains only raw data values with no metadata, field names, or type markers. Both the writer and reader must share the identical schema externally. If the schema changes between write and read, all values after the first mismatched field will be corrupted because byte offsets will no longer align. This is a fundamental property of fixed-schema binary protocols.
Both use a 2-byte uint16 length header. The difference is semantic: string encodes a JavaScript string value via TextEncoder (UTF-8) and decodes it back via TextDecoder. shortBuffer stores and returns raw Uint8Array data without text encoding. In this tool, shortBuffer fields accept hex-encoded input (e.g., 'ff00ab') and return hex on read. The buffer type is identical to shortBuffer but uses a 4-byte uint32 header allowing up to ~4 GB payloads.