txt2bin for VS Code
Write binary files as plain text, with an editor that understands them.
txt2bin turns a readable, commented .ttb text file into any binary file you want: a 3D model, a MIDI song, a PDF, a boot sector, Arduino firmware, a test fixture for your parser. Hex bytes, text, base64, integers and floats in either byte order, math expressions, labels that compute sizes and offsets for you, and inline assembly for 30+ CPU architectures can all go in the same file.
This extension adds full language support for .ttb files: highlighting, errors as you type, the offset and size of every section, and a button that writes the binary file.
<<# A playable MIDI file: a C-major arpeggio.
<<utf8
MThd
<<uint32+
6 # header length (MIDI is big-endian: "+")
<<uint16+
0 1 96 # format 0, one track, 96 ticks per beat
<<utf8
MTrk
<<uint32+
$end-$start # track length: computed for you
<<@start
<<hex
00 C0 00 # piano
00 90 3C 64 # C4 on
60 80 3C 40 # C4 off, one beat later
00 90 40 64 60 80 40 40 # E4
00 90 43 64 60 80 43 40 # G4
00 90 48 64 # C5 on...
<<uint8
(0x80 | (192 >> 7)) (192 & 0x7F) # ...for two beats (VLQ-encoded delta)
<<hex
80 48 40
00 FF 2F 00 # end of track
<<@end
Save it as song.ttb, click Generate binary file in the editor title bar, and you get a 62-byte song.mid that plays in GarageBand, MuseScore or any MIDI player. From a terminal, it is one command:
npx txt2bin@latest song.ttb song.mid
Features
- Syntax highlighting for every section type: directives, labels, text and escapes, hex bytes, base64, expressions (numbers, operators, functions,
$labels) and assembly (mnemonics, registers, local labels, comments, and ${...} shown as an expression).
- Errors as you type, on the right line, including errors from the assembler.
- Offset and size of every section, shown at the end of each
<<directive line.
- Hover over a
$label to see its value in decimal and hex; over a directive to see its offset, size and, for assembly, the generated bytes.
- Go to definition (F12 / Ctrl+click) from a
$label to its <<@label.
- Outline and breadcrumbs with every section and label.
- Commands (
Ctrl+Shift+P, category txt2bin):
- Gerar arquivo binário (Generate binary file), also a button in the editor title bar. Choose the format: raw binary, hex, base64, base64url, data URI or Intel HEX.
- Mostrar mapa de seções (Show section map): every section and label with its offset and size.
- Mostrar dump hexadecimal (Show hex dump) of the output.
- Snippets:
glb (a glTF binary skeleton), bloco (a block with a computed length), asm, boot (an x86 boot sector), @ (label) and # (comment block).
- English or Portuguese messages, following VS Code's display language.
The converter is bundled with the extension, so nothing else needs to be installed. Only <<assembly sections need LLVM (see Assembly).
A five-minute tour of the language
A .ttb file is a list of sections. Each one starts with <<directive at the start of a line and says how the lines below it become bytes.
1. Bytes, text and numbers
<<utf8
Hi!\n
<<hex
DE AD BE EF # comments anywhere
<<uint16
1 2 0x7FFF # little-endian by default
<<uint32+
0x89504E47 # add "+" for big-endian
48 69 21 0a de ad be ef 01 00 02 00 ff 7f 89 50 4e 47
2. Labels: never count bytes again
<<@name marks a position; $name is its offset. Labels can be used before they are defined, so length fields just work:
<<uint32
$end-$start # length of the block below, filled in for you
<<@start
<<utf8
{"asset":{"version":"2.0"}}
<<align 4 20 # pad with spaces to a multiple of 4
<<@end
Edit the JSON and the length updates itself, and the editor shows the new offsets as you type.
3. Math where you need it
<<float32
(sin(pi/6)) (sqrt(2)/2) 1.5e-3
<<int8
round(127*sin(tau/16)) round(127*sin(2*tau/16)) round(127*sin(3*tau/16))
<<uint8
(1 << 5) (0xF0 >> 4) 'A' (0b1010 ^ 0b0110)
<<uint64
0xFFFF_FFFF_FFFF_FFFF # integers are exact: no 64-bit precision loss
Python-style operators and precedence, 0x/0b/0o literals, _ separators, character literals and 25 math functions. Values are separated by spaces, so an expression that contains spaces goes in parentheses.
4. Machine code, inline
<<@start
<<assembly(x86_16) org=0x7c00
xor ax, ax
mov ds, ax
mov si, offset msg ; Intel syntax, ";" comments
mov ah, 0x0e ; BIOS teletype output
next:
lodsb
test al, al
jz done
int 0x10
jmp next
done:
hlt
jmp done
msg:
<<utf8
Hello from a text file!\r\n\0
<<fill 510-($.-$start)
<<uint16+
0x55AA
That is a complete, bootable x86 boot sector (type boot to get the skeleton from a snippet). ${expression} drops values from the file into the assembly, and hovering over the <<assembly line shows the bytes it produced.
5. Errors that point at the line
Every mistake is underlined where it happens: a value that doesn't fit its type, an undefined label, a stray +, a - inside standard base64 ("is this base64url?"), or an error from the assembler, translated back to your source line.
What people build with it
|
|
example |
| 3D models |
A complete .glb (glTF) file, JSON chunk and vertex buffer included, with chunk sizes computed from labels. |
gltf-face.ttb |
| Media files |
A playable MIDI song. The same approach fits WAV, BMP, PNG chunks… |
midi.ttb |
| Documents |
A valid one-page PDF whose cross-reference offsets are computed by labels. |
pdf-minimo.ttb |
| Boot sectors |
16-bit x86 code at org=0x7c00, padded to byte 510, then 55 AA. |
boot-x86.ttb |
| Microcontroller firmware |
AVR assembly that blinks an Arduino Uno's LED, written to Intel HEX for avrdude. |
arduino-blink.ttb |
| Lookup tables |
Sine tables, circle vertices, bit masks, computed in place. |
expressoes.ttb |
| Test fixtures |
Valid, almost-valid and deliberately broken inputs for your parser, readable in code review. |
|
Directives at a glance
| directive |
output |
<<utf8, <<utf16, <<utf16+ |
text as UTF-8 / UTF-16 LE / BE |
<<hex |
the hex bytes |
<<base64, <<base64url |
the decoded bytes |
<<uint8 … <<uint64, <<int8 … <<int64 |
integers, 1 to 8 bytes each |
<<float16, <<float32, <<float64 |
floating-point numbers |
<<decimal [W], <<octal [W] |
integers written as ASCII digits |
<<align N [HH] |
padding to a multiple of N |
<<fill N [HH] |
N bytes of HH |
<<@name |
nothing; defines $name |
<<# |
nothing (block comment) |
<<assembly(arch) [options] |
machine code |
Append + to any multi-byte type for big-endian. The full reference is in the project README.
Command line
The same files compile from the terminal with the txt2bin CLI (Node.js 18+ or Bun):
npx txt2bin@latest file.ttb file.bin # raw binary
npx txt2bin@latest firmware.ttb -f ihex -o blink.hex # Intel HEX for avrdude
npx txt2bin@latest file.ttb --map # sections and labels with offsets
Or install it once: npm install -g txt2bin, then txt2bin file.ttb file.bin.
Assembly
<<assembly sections are assembled with LLVM (llvm-mc, plus ld.lld when relocations need resolving):
| OS |
command |
| macOS |
brew install llvm lld |
| Debian/Ubuntu |
apt install llvm lld |
| Windows |
winget install LLVM.LLVM |
Supported out of the box: x86 (16/32/64-bit), ARM and AArch64, AVR, RISC-V, MIPS, PowerPC, SPARC, MSP430, Xtensa (ESP32), and any other LLVM target. Custom assemblers (NASM, or Z80/6502 tools) can be configured in a txt2bin.config.json next to your file; the extension reads it too.
Settings
| setting |
default |
|
txt2bin.validacao |
aoDigitar |
when to check the file: aoDigitar (as you type), aoSalvar (on save) or desligada (off) |
txt2bin.atraso |
400 |
milliseconds to wait after you stop typing |
txt2bin.mostrarOffsets |
true |
show offset and size at the end of directive lines |
Links