Encodings

Decimal

Numbers can be bytes or Unicode code points. Give the ASCII table some company
IDdecimal03 / 20decimal · 10 characters
Encoding / Decimal

Decimal

A number a byte. The ASCII table, written out.

Overhead
+156% on random bytes
Padding
none
Standard
Byte values in base 10; Unicode code points
Family
1 in decimal
family 1 / 20

Sample

Out
104 101 108 108 111 32 119 111 114 108 100 33
Back
"hello world!"

Options

codepointsboolean
default false
Each number is a Unicode code point of UTF-8 text, such as 380 for ż
separateboolean
default true
Put a space between bytes; without it each byte takes three digits. Code points are always spaced

Access

Importimport { decimal } from "@agntn/encodings/decimal"
CLIencodings encode decimal 'hello world!'
Tryplayground with the sample above

The ASCII table, written out. 72 is H, 105 is i. You've seen it a hundred times.

ts
encode("decimal", "Hi");                       // "72 105"
encode("decimal", "Hi", { separate: false });  // "072105"

Spaces or commas between numbers, both read the same. Without any separator, decimal can't tell 1 2 from 12. So separate: false pads every byte to three digits, and decoding reads a run three at a time. Four digits in a row? That's an error, not a guess.

Anything over 255 doesn't fit a byte, and you get the index of it.

Need Unicode code points instead of bytes?

Got Polish letters in your puzzle? Set codepoints: true to read each number as a Unicode code point. The decoded text comes back as UTF-8 bytes. Encoding takes UTF-8 text and writes a number for each character. Much nicer than making ż wait outside the ASCII table.

ts
decode("decimal", "90 97 380 243 322 263", { codepoints: true }).bytes;  // Uint8Array [90, 97, 197, 188, 195, 179, 197, 130, 196, 135], "Zażółć"
encode("decimal", "Zażółć", { codepoints: true });                      // "90 97 380 243 322 263"

By default, decimal reads bytes from 0 to 255. That distinction matters even below 256. 233 can be a byte or a character, depending on the option.

ts
decode("decimal", "233").bytes;                        // Uint8Array [233]
decode("decimal", "233", { codepoints: true }).bytes;  // Uint8Array [195, 169], é in UTF-8

Code points are always encoded with spaces. They have no fixed width, so separate: false doesn't apply. Decoding accepts spaces, commas and leading zeros.

The decoder refuses values above 1114111 and surrogates from 55296 to 57343. The error includes the number's index. So 55356 57225, the UTF-16 halves of 🎉 from JavaScript's charCodeAt, is refused. The whole emoji is code point 127881. In byte mode, values above 255 produce an error ending with for Unicode code points, such as 380 for ż, set codepoints.