ruby - 字符串中的字符编码

标签 ruby character-encoding

我尝试使用以下代码以转义形式(ascii 252、八进制 374、十六进制 0xfc)输出包含字母“ü”的德语句子:

pp "Test \374"
pp "Test \374".encode("UTF-8")

但是使用 ruby​​ 1.8.7 我得到: “测试\374” “测试\374”

使用 ruby​​ 1.9.2 输出: “测试\xFC” “测试\xFC”

如何让 ruby​​ (1.8.7 + 1.9.x) 输出“Test ü”? :)

最佳答案

>> pp "Test \xc3\xbc"
"Test ü"
=> nil

>> s="Test \374"  # This has utf-8 encoding but we need it to be "ISO-8859-1"
=> "Test \xFC"
>> s.force_encoding("ISO-8859-1")
=> "Test "
>> s.encode("UTF-8")
=> "Test ü"
>> 

关于ruby - 字符串中的字符编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3564477/

相关文章:

mysql - django中出现字符编码错误不正确的字符串值: '\\xF0\\x9F\\x

php - 我如何使用 php 显示来自 mysql 数据库的非英文字符?

html - jsFiddle 字符集

使用希腊语进行 PHP 编码

ruby-on-rails - Rails 3. 无法在任何来源中找到 libv8-3.3.10.4

ruby-on-rails - 在 graphql 分页中找到名称 'Connection' 的重复类型定义

ruby-on-rails - 如何使用 Paperclip 为我的附件指定文件名?

python - String.encode 和 codecs.encode 之间有区别吗?

ruby-on-rails - 使用 acts-as-taggable-on ruby​​gem 优化查询

Ruby 未定义方法 `[]' 为 nil :NilClass (NoMethodError)