Jump to content

Netstring

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Scarfboy (talk | contribs) at 23:12, 24 March 2008. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

In computer programming, a netstring refers to a self-delimited way of encoding a string, using the length of the bytestring that follows, allowing unambiguous passing around of byte data that may, for example, include what could otherwise be interpreted as a terminator (such as a null character).

Netstrings are described in a document by D. J. Bernstein.


The format consists of the string's length written using ASCII digits, followed by a colon, the data, and a comma. For example, "hello world!" encodes as:

12:hello world!,

And an empty string as:

0:,

The comma makes it slightly simpler for humans to read netstrings that are used as adjacent records, and provides weak verification of correct parsing. Note that without the comma, the format mirrors how Bencode encodes strings.


The format is simple to generate and to parse, and make it possible to know how much memory to allocate before all data is received, which can be useful in networking and other streaming contexts.

Netstrings avoid complications such as content-dependent delimiting present, for example, in multipart MIME messages, but since no limitations are posed on the contents of the string, netstrings can not be embedded in any delimited format without the possibility of them interfering with such delimiting.

Netstrings are primarily useful for simple data exchanges, for example to exactly transmit (byte)strings between programs in different languages without having to solve worries about details like null termination and newlines without having to resort to solutions like custom binary formats.

Netstrings are used, for example, in the Simple Common Gateway Interface (SCGI) and the Quick Mail Queuing Protocol (QMQP).