Character encoding in a string

I am trying to infer a German setting containing the letter "ü" in escaped form (ascii 252, octal 374, hex 0xfc) using the following code:

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

But using ruby ​​1.8.7 I get: "Test \ 374" "Test \ 374"

Using ruby ​​1.9.2 outputs: "Test \ xFC" "Test \ xFC"

How can I get ruby ​​(1.8.7 + 1.9.x) to output "Test ü"? :)

+3
source share
1 answer
>> 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 ü"
>> 
+6
source

Source: https://habr.com/ru/post/1761481/


All Articles