beetlebug▾
/

encoding convertto

Gives the bytes of a string in an encoding, as a byte string.

encoding convertto ?-profile PROFILE? ?-failindex VAR? ENCODING STRING
encoding convertto STRING

Description

Converts the text STRING to its bytes in ENCODING, and gives them as a byte string: one character below U+0100 for each byte. With one argument, ENCODING is the encoding of encoding system, so encoding convertto utf-8 gives the bytes of the text utf-8.

A character that ENCODING has no code for is an error in Tcl 9.1, with the index of the character in the message and in the error code. The profile replace and the profile tcl8 put a ? in its place, and -failindex gives the bytes before it. In Tcl 8.6 it becomes ?.

Arguments

ArgumentTypeDefaultDescription
ENCODINGencoding namethe system encodingThe encoding of the bytes, a name of encoding names.
STRINGstringrequiredThe text to convert.

Options

OptionTypeDefaultDescription
-profileprofile namestrictSets what a character with no code in ENCODING gives. Tcl 9.1 only
-failindexvariable namenoneGives the bytes before the first character with no code, and puts its index in a variable. Tcl 9.1 only

-profile PROFILE Tcl 9.1 only

Sets what a character with no code in ENCODING gives.

With strict, a character with no code is an error. With replace and with tcl8, it gives the byte of ?.

Values: replace, strict, tcl8.

% binary encode hex [encoding convertto -profile replace iso8859-1 a€]
613f

% binary encode hex [encoding convertto -profile tcl8 ascii aé]
613f
% encoding convertto -profile strict iso8859-1 €
unexpected character at index 0: 'U+0020AC'
-errorcode TCL ENCODING ILLEGALSEQUENCE 0

% encoding convertto -profile bogus utf-8 a
bad profile name "bogus": must be replace, strict, or tcl8
-errorcode TCL ENCODING PROFILE bogus

-failindex VAR Tcl 9.1 only

Gives the bytes before the first character with no code, and puts its index in a variable.

With -failindex, a character with no code is no error: the result is the bytes of the characters before it, and VAR gets its index in STRING. When each character converts, VAR gets -1.

% encoding convertto -failindex j iso8859-1 a€b
a

% encoding convertto -failindex j iso8859-1 a€b; set j
1

% encoding convertto -failindex j iso8859-1 ab; set j
-1
% encoding convertto -nocomplain utf-8 a
bad option "-nocomplain": must be -profile or -failindex
-errorcode TCL LOOKUP INDEX option -nocomplain

Returns

A byte string.

Examples

% binary encode hex [encoding convertto iso8859-1 é]
e9

% binary encode hex [encoding convertto unicode é]
e900

# With one argument, the encoding is the system encoding, utf-8.
% binary encode hex [encoding convertto é]
c3a9

# Tcl 8.6 only
% binary encode hex [encoding convertto ascii é]
3f

Errors

# Tcl 9.1 only
% encoding convertto ascii é
unexpected character at index 0: 'U+0000E9'
-errorcode TCL ENCODING ILLEGALSEQUENCE 0

% encoding convertto bogus abc
unknown encoding "bogus"
-errorcode TCL LOOKUP ENCODING bogus

# Tcl 9.1 only
% encoding convertto
wrong # args: should be "encoding convertto ?-profile profile? ?-failindex var? encoding data" or "encoding convertto data"
-errorcode TCL WRONGARGS

# Tcl 8.6 only
% encoding convertto
wrong # args: should be "encoding convertto ?encoding? data"
-errorcode TCL WRONGARGS

Facts

Capability
channels. A new interpreter does not have it until the host turns it on.
Tcl release
Tcl 9.1 and 8.6.
Since
ztcl 1.0.0-beta.1

See also