beetlebug▾
/

encoding convertfrom

Gives the text of the bytes of a byte string in an encoding.

encoding convertfrom ?-profile PROFILE? ?-failindex VAR? ENCODING BYTES
encoding convertfrom BYTES

Description

Reads BYTES, a byte string, as bytes in ENCODING, and gives the text. With one argument, ENCODING is the encoding of encoding system.

A byte or a sequence of bytes that is not valid in ENCODING is an error in Tcl 9.1, with the index of the first bad byte in the message and in the error code. The profile replace gives U+FFFD in its place, the profile tcl8 gives each bad byte as the character of the same code, and -failindex gives the text before it. In Tcl 8.6 a bad byte gives the character of the same code.

A character of BYTES at U+0100 or above is no byte, and ztcl reads the low 8 bits of its code.

Arguments

ArgumentTypeDefaultDescription
ENCODINGencoding namethe system encodingThe encoding of the bytes, a name of encoding names.
BYTESbyte stringrequiredThe bytes to read.

Options

OptionTypeDefaultDescription
-profileprofile namestrictSets what a byte that is not valid in ENCODING gives. Tcl 9.1 only
-failindexvariable namenoneGives the text before the first bad byte, and puts its index in a variable. Tcl 9.1 only

-profile PROFILE Tcl 9.1 only

Sets what a byte that is not valid in ENCODING gives.

With strict, a bad byte is an error. With replace, it gives U+FFFD. With tcl8, it gives the character of the same code, as Tcl 8.6 does.

Values: replace, strict, tcl8.

% encoding convertfrom -profile replace utf-8 a\xffb
a�b

% encoding convertfrom -profile tcl8 utf-8 \xff
ÿ
% encoding convertfrom -profile strict utf-8 [binary decode hex c0]
unexpected byte sequence starting at index 0: '\xC0'
-errorcode TCL ENCODING ILLEGALSEQUENCE 0

% encoding convertfrom -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 text before the first bad byte, and puts its index in a variable.

With -failindex, a bad byte is no error: the result is the text of the bytes before it, and VAR gets its index in BYTES. When each byte is valid, VAR gets -1. With -profile replace no byte fails, so VAR gets -1.

% encoding convertfrom -failindex i utf-8 a\xffb
a

% encoding convertfrom -failindex i utf-8 a\xffb; set i
1

% encoding convertfrom -failindex i utf-8 ab; set i
-1
% encoding convertfrom -bogus x utf-8 a
bad option "-bogus": must be -profile or -failindex
-errorcode TCL LOOKUP INDEX option -bogus

Returns

The text.

Examples

% encoding convertfrom utf-8 [binary decode hex c3a9]
é

% encoding convertfrom iso8859-1 \xe9
é

% encoding convertfrom unicode [binary decode hex e900]
é

# Tcl 8.6 only
% encoding convertfrom utf-8 \xff
ÿ

Errors

# Tcl 9.1 only
% encoding convertfrom utf-8 \xff
unexpected byte sequence starting at index 0: '\xFF'
-errorcode TCL ENCODING ILLEGALSEQUENCE 0

# Tcl 9.1 only
# The text é is the byte E9. That byte alone is not valid UTF-8.
% encoding convertfrom utf-8 é
unexpected byte sequence starting at index 0: '\xE9'
-errorcode TCL ENCODING ILLEGALSEQUENCE 0

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

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