Tcl字符串编码命令encoding

本文详细介绍了Tcl中字符串的编码方式,包括modifiedUTF-8、16位Unicode和binary。探讨了如何使用Tcl的encoding命令进行字符串的编码转换,如convertfrom和convertto子命令,以及如何设置和查询系统编码。

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值