Overview
Introduction
Delimiter-based text formats need escaping whenever the data itself contains the delimiter. Netstrings sidestep that entirely by stating the exact byte length up front, so a parser always knows precisely how much data to read.
This tool encodes arbitrary text into that format in one step.
What Is Netstring Encoder?
A netstring encoder that measures the UTF-8 byte length of your input and wraps it as `<length>:<data>,`.
It's a simple, self-delimiting alternative to newline- or null-terminated strings, most associated with Bernstein's own tools and later protocols like tnetstrings.
How Netstring Encoder Works
The tool encodes the input to UTF-8 bytes to get an accurate byte count (not a character count), then formats the result as the decimal length, a colon, the original text, and a trailing comma.
No escaping of the data itself is needed or performed, since the length prefix alone tells a decoder exactly where the data ends.
When To Use Netstring Encoder
Use it when implementing or debugging a protocol that uses netstring framing for messages.
It's also a quick way to see the exact UTF-8 byte length of a string, since that number is embedded directly in the output.
Often used alongside Netstring Decoder and String to Bytes Converter.
Features
Advantages
- No escaping needed, even for data containing the delimiter characters.
- Byte length is measured accurately via UTF-8 encoding, not character count.
- Simple, unambiguous format.
Limitations
- Only useful when the destination system actually expects netstring framing.
- Byte length must be recomputed if the text is edited; it isn't kept in sync automatically.
Examples
Best Practices & Notes
Best Practices
- Verify your target system expects byte length, not character count, before comparing lengths by hand.
- Use Convert a Netstring to a String to confirm decoding round-trips correctly.
- Encode the exact bytes you intend to transmit, since any later edit to the text invalidates the length prefix and breaks the framing.
Developer Notes
Length is computed with `new TextEncoder().encode(input).length` rather than `input.length`, since the latter counts UTF-16 code units, which diverges from UTF-8 byte length for any character outside the Basic Multilingual Plane or with a multi-byte UTF-8 encoding.
Netstring Encoder Use Cases
- Framing a message for a protocol that uses netstring encoding
- Debugging a tnetstrings-based service
- Measuring a string's exact UTF-8 byte length
Common Mistakes
- Assuming character count and byte length are always equal.
- Forgetting the trailing comma when hand-constructing a netstring.
Tips
- Pair with String to Bytes if you need to inspect the raw encoded bytes directly.
- The trailing comma is part of the format rather than an optional separator; a netstring missing it will be rejected by a conforming parser.