字符串中的字符编码

时间:2010-08-25 09:31:29

标签: ruby character-encoding

我尝试使用以下代码以转义形式(ascii 252,octal 374,hex 0xfc)输出包含字母“ü”的德语句子:

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

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

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

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

1 个答案:

答案 0 :(得分:7)

>> 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 ü"
>>