【发布时间】:2023-08-03 16:11:01
【问题描述】:
谁能解释这段代码发生了什么?
s1 = "\x20".force_encoding 'UTF-8'
s2 = "\x20".force_encoding 'ASCII-8BIT'
puts "s1 == s2: #{s1 == s2}"
s3 = "\xAB".force_encoding 'UTF-8'
s4 = "\xAB".force_encoding 'ASCII-8BIT'
puts "s3 == s4: #{s3 == s4}"
在 Ruby 2.0.0p353 中打印:
s1 == s2: true
s3 == s4: false
我不明白为什么 s1 和 s2 相等时 s3 和 s4 不相等。 0xAB 是 '½' 的 ASCII 码,据我所知,它可以用 ASCII-8BIT 和 UTF8 表示。
【问题讨论】:
-
\0xAB也是 not½作为 UTF-8 字符代码。我发现了这个:"\xAB".force_encoding('CP850').encode('UTF-8')- 给了½。 . . en.wikipedia.org/wiki/Code_page_850 - 可能其他一些基于 MSDOS 的扩展也有这个映射。 -
我不知道你从哪里得到关于 1/2 的 ASCII 码的信息。它实际上是Left-pointing double angle quotation mark, left pointing guillemet。你的意思是
\xBD? -
感谢@NeilSlater,这很有道理!
-
0xAB 不是 ASCII,[0xAB] 不是有效的 UTF-8 字符串。
标签: ruby utf-8 character-encoding ascii-8bit