string 用于获取字符串中每个字符的 ASCII 码的 Tcl
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1675677/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Tcl for getting ASCII code for every character in a string
提问by Dexygen
I need to get the ASCII character for every character in a string. Actually its every character in a (small) file. The following first 3 lines successfully pull all a file's contents into a string (per this recipe):
我需要为字符串中的每个字符获取 ASCII 字符。实际上它是(小)文件中的每个字符。下面的前 3 行成功地将文件的所有内容提取到一个字符串中(根据本节):
set fp [open "store_order_create_ddl.sql" r]
set data [read $fp]
close $fp
I believe I am correctly discerning the ASCII code for the characters (see http://wiki.tcl.tk/1497). However I'm having a problem figuring out how to loop over every character in the string.
我相信我正确识别了字符的 ASCII 代码(参见http://wiki.tcl.tk/1497)。但是,我在弄清楚如何遍历字符串中的每个字符时遇到了问题。
First of all I don't think the following is an especially idiomatic way of looping over characters in a string with Tcl. Second and more importantly, it behaves incorrectly, inserting an extra element between every character.
首先,我不认为以下是使用 Tcl 循环字符串中字符的特别惯用的方式。其次,更重要的是,它的行为不正确,在每个字符之间插入了一个额外的元素。
Below is the code I've written to act on the contents of the "data" variable set above, followed by some sample output.
下面是我为处理上面设置的“数据”变量的内容而编写的代码,后面是一些示例输出。
CODE:
代码:
for {set i 0} {$i < [string length $data]} {incr i} {
set char [string index $data $i]
scan $char %c ascii
puts "char: $char (ascii: $ascii)"
}
OUTPUT:
输出:
char: C (ascii: 67)
char: (ascii: 0)
char: R (ascii: 82)
char: (ascii: 0)
char: E (ascii: 69)
char: (ascii: 0)
char: A (ascii: 65)
char: (ascii: 0)
char: T (ascii: 84)
char: (ascii: 0)
char: E (ascii: 69)
char: (ascii: 0)
char: (ascii: 32)
char: (ascii: 0)
char: T (ascii: 84)
char: (ascii: 0)
char: A (ascii: 65)
char: (ascii: 0)
char: B (ascii: 66)
char: (ascii: 0)
char: L (ascii: 76)
char: (ascii: 0)
char: E (ascii: 69)
回答by RHSeeger
The following code should work:
以下代码应该可以工作:
set data {CREATE TABLE}
foreach char [split $data ""] {
lappend output [scan $char %c]
}
set output ;# 67 82 69 65 84 69 32 84 65 66 76 69
As far as the extra characters in your output, it seems like the problem is with your input data from the file. Is there some reason there would be null characters (\0) in between every character in the file?
至于输出中的额外字符,问题似乎出在文件中的输入数据上。是否有某种原因在文件中的每个字符之间会有空字符 (\0)?
回答by C. M.
Came across this older question while looking for something else.. Going to answer it for the benefit of anyone else who may be looking for an answer to this question..
在寻找其他东西时遇到了这个较旧的问题..为了可能正在寻找这个问题答案的任何其他人的利益而回答它..
First off, understand what character encodings are. The source data in the example is NOT ASCII character encoding, so the ASCII character codes (codes 0-127) really have no meaning--Except in this example, the encoding appears to be UTF-16, which includes ASCII codes as a subset. What you probably want is the full range of "character" codes from 0 to 255, but depending on your system, the source of the data, etc, codes 128-255 may be ANSI, ISO, or some other strange code page. What you want to do is convert the data in to a format you know how to handle, such as the very common ISO 8859-1 code (encoding "iso8859-1"), which is very similar to Windows 1252 standard encoding (encoding "cp1252"), or UTF-8 (encoding "utf-8") with the "encoding" command:
首先,了解什么是字符编码。示例中的源数据不是 ASCII 字符编码,所以 ASCII 字符代码(代码 0-127)确实没有意义--除了本示例中,编码似乎是 UTF-16,其中包括 ASCII 代码作为子集. 您可能想要的是从 0 到 255 的完整范围的“字符”代码,但是根据您的系统、数据源等,代码 128-255 可能是 ANSI、ISO 或其他一些奇怪的代码页。您要做的是将数据转换成您知道如何处理的格式,例如非常常见的 ISO 8859-1 代码(编码“iso8859-1”),它与 Windows 1252 标准编码(编码“ cp1252") 或 UTF-8(编码“utf-8”)与“编码”命令:
set data [encoding convertto utf-8 $data] ;# For UTF-8
set data [encoding convertto utf-8 $data] ;# 对于 UTF-8
set data [encoding convertto iso8859-1 $data] ;# For ISO 8859-1
set data [encoding convertto iso8859-1 $data] ;# 对于 ISO 8859-1
and so on. If you're reading the data from a file, you may want to set the file encoding (via fconfigure) prior to reading the data as well, to make sure you're reading the file data correctly. Look up the man pages for "encoding" (and "fconfigure") for more details on handing character set encoding.
等等。如果您从文件中读取数据,您可能还需要在读取数据之前设置文件编码(通过 fconfigure),以确保正确读取文件数据。查看“编码”(和“fconfigure”)的手册页以获取有关处理字符集编码的更多详细信息。
Once you have the encoding of the data under control, the rest of the example code should work as expected.
一旦您控制了数据的编码,示例代码的其余部分就会按预期工作。