Websites Navigation: Airbit | Shop | m-shell.net
Languages: EN | DE

Module encoding: Standard Encodings

This module provides functions to convert to and from often used encodings of data (text or binary). The following encodings are currently supported:

NameBitsDescription
base648/6Base-64 encoding for transmission via text oriented protocols (e.g. e-mail).
hex8/4Hexadecimal encoding for 8-bit human readable binary data.
hex1616/4Hexadecimal encoding for 16-bit human readable binary data (big endian).
html16/7HTML character entity encoding.
uri8/7URI component encoding for e.g. URL parameters.
utf816/8UTF-8 encoding for international characters.

For each encoding xxx, there is a fromxxx function decoding encoded data, and a toxxx function encoding raw data. All functions convert between strings.

See also: .char, .code.

encoding.frombase64

• function frombase64(s) → String

Converts the Base-64 encoded string s into 8-bit binary data. This is the inverse of encoding.tobase64.

Throws ErrArgument if the string length is not a multiple of four, or contains a non-Base-64 character.

encoding.fromhex

• function fromhex(s) → String

Converts the hexadecimally encoded string s into 8-bit binary data. This is the inverse of encoding.tohex.

Throws ErrArgument if the string length is not a multiple of two, or contains an non-hexadecimal character.

encoding.fromhex16

• function fromhex16(s) → String

Converts the hexadecimally encoded string s into 16-bit binary data. This is the inverse of encoding.tohex16.

Throws ErrArgument if the string length is not a multiple of four, or contains an non-hexadecimal character.

encoding.fromhtml

• function fromhtml(s) → String

Converts the HTML encoded string s into 16-bit data. This is the inverse of encoding.tohtml. The following HTML character entities are converted:

&Ampersand character &.
>Greater than character >.
&lt;Less than character <.
&quot;Double quote character ".
&#n;Character with code n.

All other encoded character entities are converted to a question mark character ?.

Throws ErrArgument if the string contains an incomplete character entity.

encoding.fromuri

• function fromuri(s) → String

Converts the URI-encoded string s into 8-bit binary data. This is the inverse of encoding.touri.

Throws ErrArgument if the string contains a % character without two following hexadecimal digits.

encoding.fromutf8

• function fromutf8(s) → String

Converts the UTF8-encoded string s into 16-bit binary data. This is the inverse of encoding.toutf8.

Throws ErrArgument if the string contains an invalid UTF-8 character.

encoding.tobase64

• function tobase64(s) → String

Converts the string s (8-bit part of characters only) into a Base-64 encoded string.

t=encoding.tobase64("un château français")
print t
→ dW4gY2jidGVhdSBmcmFu52Fpcw==
print encoding.frombase64(t)
→ un château français

encoding.tohex

• function tohex(s) → String

Converts the string s (8-bit part of characters only) into a hexadecimally encoded string with two digits per character.

t=encoding.tohex("un château français")
print t
→ 756e206368e274656175206672616ee7616973
print encoding.fromhex(t)
→ un château français

encoding.tohex16

• function tohex16(s) → String

Converts the string s (complete characters) into a hexadecimally encoded string with four digits per character, high byte first (big endian).

t=encoding.tohex16("\u42f2\u13b7ab")
print t
→ 42f213b700610062
print encoding.fromhex16(t)
→ __ab

encoding.tohtml

• function tohtml(s) → String

Converts the string s into a string with valid HTML character entities[4].

t=encoding.tohtml("5<7 & 6>4\nThis is true")
print t
→ 5&lt;7 &amp; 6&gt;4&#10;This is true
print encoding.fromhtml(t)
→ 5<7 & 6>4
This is true

encoding.touri

• function touri(s) → String

Converts the string s (8-bit part of characters only) into a URI encoded string with spaces encoded as + and non-alphanumeric characters encoded in hexadecimal with % prefix.

To encode a 16-bit string, the URI encoding is often applied to a UTF8-encoded string.

t=encoding.touri("un château français")
print t
→ un+ch%E2teau+fran%E7ais
print encoding.fromuri(t)
→ un château français
// do the same with intermediate UTF-8 encoding
t=encoding.touri(encoding.toutf8("un château français"))
print t
→ un+ch%C3%A2teau+fran%C3%A7ais
print encoding.fromutf8(encoding.fromuri(t))
→ un château français

encoding.toutf8

• function toutf8(s) → String

Converts the string s into an UTF-8 encoded string.

t=encoding.toutf8("un château français")
print t
→ un chÃ_teau franÃ_ais
print encoding.fromutf8(t)
→ un château français


© 2004-2011 airbit AG, CH-8008 Zürich
Document AB-M-LIB-888
mShell Home  > Documentation  > Manuals