Tag: iconv

Ruby转换字符串编码从ISO-8859-1到UTF-8不起作用

我试图将一个字符串从ISO-8859-1编码转换为UTF-8,但我似乎无法让它工作。 这是我在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” 我不确定为什么iso-8859-1中的Norrlandsvägen将在utf-8中转换为Norrlandsvägen 。 我已经尝试过编码,编码!,编码(destinationEncoding,originalEncoding),iconv,force_encoding,以及我能想到的各种奇怪的解决方法,但似乎没有任何效果。 有人可以帮助我/指出我正确的方向吗? Ruby新手仍然像疯了一样拉头发,但感谢所有回复… 🙂 这个问题的背景:我正在编写一个gem,它将从一些网站下载一个xml文件(将具有iso-8859-1编码)并将其保存在存储中,我想先将其转换为utf-8。 但像Norrlandsvägen这样的词语让我感到困惑 。 真的任何帮助将不胜感激! [更新]:我意识到在irb控制台中运行这样的测试可能会给我不同的行为,所以这里是我在实际代码中的内容: def convert_encoding(string, originalEncoding) puts “#{string.encoding}” # ASCII-8BIT string.encode(originalEncoding) puts “#{string.encoding}” # still ASCII-8BIT string.encode!(‘utf-8’) end 但最后一行给出了以下错误: Encoding::UndefinedConversionError – “\xC3” from ASCII-8BIT to UTF-8 感谢\xC3在下面的回答,我注意到如果你运行的话, \xC3实际上会显示在irb中: irb(main):001:0> […]

在`require’:没有这样的文件加载 – iconv(LoadError)

➜ expertiza git:(master) ✗ ruby -v ruby 1.8.7 (2011-06-30 patchlevel 352) [i686-darwin11.1.0] ➜ expertiza git:(master) ✗ rails -v Rails 2.3.14 ➜ expertiza git:(master) ✗ script/server /Users/HPV/.rvm/gems/ruby-1.8.7-p352/gems/activesupport-2.3.14/lib/active_support/inflector.rb:3:in `require’: no such file to load — iconv (LoadError) from /Users/HPV/.rvm/gems/ruby-1.8.7-p352/gems/activesupport-2.3.14/lib/active_support/inflector.rb:3 from /Users/HPV/.rvm/gems/ruby-1.8.7-p352/gems/activesupport-2.3.14/lib/active_support/core_ext/integer/inflections.rb:1:in `require’ from /Users/HPV/.rvm/gems/ruby-1.8.7-p352/gems/activesupport-2.3.14/lib/active_support/core_ext/integer/inflections.rb:1 from /Users/HPV/.rvm/gems/ruby-1.8.7-p352/gems/activesupport-2.3.14/lib/active_support/core_ext/integer.rb:2:in `require’ from /Users/HPV/.rvm/gems/ruby-1.8.7-p352/gems/activesupport-2.3.14/lib/active_support/core_ext/integer.rb:2 from /Users/HPV/.rvm/gems/ruby-1.8.7-p352/gems/activesupport-2.3.14/lib/active_support/core_ext.rb:8:in `require’ from /Users/HPV/.rvm/gems/ruby-1.8.7-p352/gems/activesupport-2.3.14/lib/active_support/core_ext.rb:8 from /Users/HPV/.rvm/gems/ruby-1.8.7-p352/gems/activesupport-2.3.14/lib/active_support/core_ext.rb:8:in `each’ […]