JavaScript Data to Binary Converter
Convert JavaScript data types (strings, numbers, arrays, objects, booleans) to binary representation with hex dump, bit view, and downloadable .bin files.
About
Binary serialization of JavaScript data is a non-trivial operation. Native JSON.stringify produces UTF-16 text, not compact binary. This tool implements a tagged binary packing protocol: each value is prefixed with a 1-byte type tag followed by a variable-length size prefix (LEB128 varint) and the raw payload. Strings are encoded as UTF-8 via the Web TextEncoder API. Numbers use IEEE 754 Float64 (8 bytes, big-endian). Booleans collapse to a single byte. Arrays and objects are encoded recursively with an element count prefix. Incorrect binary packing causes data corruption, protocol mismatches, and silent deserialization failures in networked systems. This tool validates input, shows the exact byte layout, and lets you download the packed binary for inspection or integration testing.
Limitations: circular references are rejected. BigInt and Symbol types are not supported. Maximum input size is approximately 1 MB of JSON text. The packing format is a custom schema designed for clarity, not a standard like Protocol Buffers or MessagePack. Pro tip: compare the packed size against JSON.stringify output to quantify the byte savings for your specific data shape.
Formulas
The binary packing protocol encodes each JavaScript value as a tagged byte sequence. The general frame structure is:
Where T = type tag (1 byte), L = length prefix (LEB128 varint, 1 - 3 bytes), V = raw value payload.
Varint encoding uses LEB128 (Little-Endian Base 128). Each byte stores 7 data bits. The high bit indicates continuation:
Numbers are encoded as IEEE 754 double-precision floating point (64 bits, big-endian):
Where s = sign bit (1 bit), e = biased exponent (11 bits), f = fractional mantissa (52 bits). Total packed size for a composite value is computed recursively:
Where n = number of elements, Li = payload length of element i, and varint_len returns the number of bytes needed to encode the length as a varint.
Reference Data
| JS Type | Type Tag (Hex) | Tag (Dec) | Payload Format | Size (Bytes) | Notes |
|---|---|---|---|---|---|
| null | 0x00 | 0 | None | 1 | Tag only, no payload |
| Boolean (false) | 0x01 | 1 | 0x00 | 2 | Tag + 1 byte |
| Boolean (true) | 0x01 | 1 | 0x01 | 2 | Tag + 1 byte |
| Number (Int8) | 0x02 | 2 | IEEE 754 Float64 BE | 9 | Tag + 8 bytes always |
| Number (Float64) | 0x02 | 2 | IEEE 754 Float64 BE | 9 | All numbers use Float64 |
| Number (NaN) | 0x02 | 2 | 7FF8000000000000 | 9 | Canonical NaN encoding |
| Number (Infinity) | 0x02 | 2 | 7FF0000000000000 | 9 | Positive infinity |
| String (empty) | 0x03 | 3 | Varint(0) | 2 | Tag + length varint |
| String (ASCII) | 0x03 | 3 | Varint(len) + UTF-8 | 2+n | 1 byte per ASCII char |
| String (Unicode) | 0x03 | 3 | Varint(len) + UTF-8 | 2-4+n | Multi-byte UTF-8 sequences |
| Array | 0x04 | 4 | Varint(count) + elements | Variable | Recursive encoding |
| Object | 0x05 | 5 | Varint(count) + key-value pairs | Variable | Keys encoded as strings |
| undefined | 0x06 | 6 | None | 1 | Tag only, no payload |
| Varint (1 byte) | Size β€ 127 | 0xxxxxxx | 1 | High bit = 0 means final | |
| Varint (2 bytes) | Size β€ 16383 | 1xxxxxxx 0xxxxxxx | 2 | LEB128 continuation bit | |
| Varint (3 bytes) | Size β€ 2097151 | 1xxxxxxx 1xxxxxxx 0xxxxxxx | 3 | Max supported length | |
| IEEE 754 Float64 | Sign 1 bit | Exponent 11 bits + Mantissa 52 bits | 8 | Big-endian byte order | |
| UTF-8 (1-byte) | U+0000 to U+007F | 0xxxxxxx | 1 | ASCII compatible | |
| UTF-8 (2-byte) | U+0080 to U+07FF | 110xxxxx 10xxxxxx | 2 | Latin, Greek, Cyrillic | |
| UTF-8 (3-byte) | U+0800 to U+FFFF | 1110xxxx 10xxxxxx 10xxxxxx | 3 | CJK, most symbols | |
| UTF-8 (4-byte) | U+10000 to U+10FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx | 4 | Emoji, rare scripts | |