I am trying to convert a string from ISO-8859-1 to UTF-8, but I cannot get it to work. Here is an example of what I did in irb.
irb(main):050:0> string = 'Norrlandsvägen' => "Norrlandsvägen" irb(main):051:0> string.force_encoding('iso-8859-1') => "Norrlandsv\xC3\xA4gen" irb(main):052:0> string = string.encode('utf-8') => "Norrlandsvägen"
I am not sure why Norrlandsvägen in iso-8859-1 will be converted to Norrlandsvägen in utf-8.
I tried to encode, encode !, encode (destinationEncoding, originalEncoding), iconv, force_encoding and all kinds of weird workarounds that I could think of, but nothing works. Can someone help me / point me in the right direction?
A ruby newbie still pulls her hair like crazy, but feels grateful for all the answers here ... :)
Background: I am writing a gem that will download an xml file from some sites (which will have the iso-8859-1 encoding) and save it to the repository, and I would like to convert it to utf-8 first. But words like Norrlandsvägen keep scaring me. In fact, any help would be greatly appreciated!
[UPDATE]: I realized that running tests like this in the irb console might give me different behavior, so here is what I have in my actual code:
def convert_encoding(string, originalEncoding) puts "#{string.encoding}"
but the last line gives me the following error:
Encoding::UndefinedConversionError - "\xC3" from ASCII-8BIT to UTF-8
Thanks @Amadan answer below, I noticed that \xC3 is actually displayed in irb if you run:
irb(main):001:0> string = 'ä' => "ä" irb(main):002:0> string.force_encoding('iso-8859-1') => "\xC3\xA4"
I also tried to assign a new variable to the string.encode(originalEncoding) result, but got an even more terrible error:
newString = string.encode(originalEncoding) puts "#{newString.encoding}" # can't even get to this line... newString.encode!('utf-8')
and Encoding::UndefinedConversionError - "\xC3" to UTF-8 in conversion from ASCII-8BIT to UTF-8 to ISO-8859-1
I was still completely lost in all this coding, but I am very grateful for all the answers and helped everyone to give me! Thanks a ton! :)