encoding
encoding option ?arg arg ...?
Tcl中的字符串在逻辑上是由16位Unicode字符组成的序列。字符串在内存中以字节序列的形式表示,字节的编码可以是“modified UTF-8”、16位 “Unicode”和“binary”。Tcl不保证总是对相同的字符串使用相同的编码。encoding执行的操作通过选项option决定。
选项
convertfrom
encoding convertfrom ?encoding? data
将数据从指定的编码转换为Unicode。data中的字符视为二进制数据,每个字符的低八位做为一个字节。所得到的字节序列被视为指定编码的字符串。如果没有指定encoding,则使用当前系统编码。
convertto
encoding convertto ?encoding? string
将字符串从Unicode转换为指定的编码。结果为表示转换后字符串的字节序列。每个字节存储在Unicode字符的低8位。如果没有指定encoding,则使用当前系统编码。
dirs
encoding dirs ?directoryList?
Tcl可以从文件系统中加载描述要使用的额外编码的编码数据文件。此命令的搜索路径为directoryList下的*.enc。如果未指定directoryList,则返回组成当前搜索路径的目录列表。如果directoryList不是有效的列表则会产生错误。如果在搜索编码数据文件时,directoryList中的元素没有指向可读、可搜索的目录,则忽略该元素。
names
encoding names
返回一个包含当前可用的所有编码的名称的列表。
system
encoding system ?encoding?
设置系统的编码为encoding。如果没有指定encoding,则使用当前系统编码。Tcl将字符串传递给系统调用时,都会使用系统编码。
简单示例
encoding dirs
encoding names
encoding system
set encoded [encoding convertto utf-8 编码测试]
encoding convertfrom utf-8 $encoded