Ruby 上的 Iconv 和 Kconv (1.9.2)

发布于 2024-11-17 09:22:52 字数 145 浏览 1 评论 0原文

我知道 Iconv 用于转换字符串的编码。 根据我的理解,Kconv 是出于同样的目的(我错了吗?)。

我的问题是:它们之间有什么区别,我应该使用什么来进行编码转换。

顺便说一句,发现一些信息表明 Iconv 将从 1.9.3 版本开始被弃用。

I know that Iconv is used to convert strings' encoding.
From my understandings Kconv is for the same purpose (am I wrong?).

My question is: what is the difference between them, and what should I use for encoding conversions.

btw found some info that Iconv will be deprecated from 1.9.3 version.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月如刀 2024-11-24 09:22:52

正如https://stackoverflow.com/users/23649/jtbandes所说,它看起来Kconv类似于 Iconv 但专门用于汉字(“现代日语书写系统中与平假名一起使用的表意汉字”http://en.wikipedia.org/wiki/Kanji)。除非您正在研究专门的日语内容,否则我猜您不需要 Kconv

如果您使用的是 Ruby 1.9,则大多数时候可以使用内置编码支持,而不是 Iconv。我花了几个小时试图理解自己在做什么,直到读到以下内容:

http://www.joelonsoftware .com/articles/Unicode.html

使用类似的东西了

String#encode           # Ruby 1.9
String#encode!          # Ruby 1.9
String#force_encoding   # Ruby 1.9

然后你就可以开始放心地 。如果您有更复杂的需求,请阅读http://blog.grayproducts.net/categories/character_encodings

更新 感谢 JohnZ 的评论

Iconv 在 Ruby 1.9 中仍然有用,因为它可以音译字符(这是 String#encode 等人无法做到的)。下面是如何使用音译为 UTF-8 的函数扩展 String 的示例:

require 'iconv'
class ::String
  # Return a new String that has been transliterated into UTF-8
  # Should work in Ruby 1.8 and Ruby 1.9 thanks to http://po-ru.com/diary/fixing-invalid-utf-8-in-ruby-revisited/
  def as_utf8(from_encoding = 'UTF-8')
    ::Iconv.conv('UTF-8//TRANSLIT', from_encoding, self + ' ')[0..-2]
  end
end

"foo".as_utf8 #=> "foo"
"foo".as_utf8('ISO-8859-1') #=> "foo"

谢谢 JohnZ!

As https://stackoverflow.com/users/23649/jtbandes says, it looks Kconv is like Iconv but specialized for Kanji ("the logographic Chinese characters that are used in the modern Japanese writing system along with hiragana" http://en.wikipedia.org/wiki/Kanji). Unless you are working on something specifically Japanese, I'm guessing you don't need Kconv.

If you're using Ruby 1.9, you can use the built-in encoding support most of the time instead of Iconv. I tried for hours to understand what I was doing until I read this:

http://www.joelonsoftware.com/articles/Unicode.html

Then you can start to use stuff like

String#encode           # Ruby 1.9
String#encode!          # Ruby 1.9
String#force_encoding   # Ruby 1.9

with confidence. If you have more complex needs, do read http://blog.grayproductions.net/categories/character_encodings

UPDATED Thanks to JohnZ in the comments

Iconv is still useful in Ruby 1.9 because it can transliterate characters (something that String#encode et al. can't do). Here's an example of how to extend String with a function that transliterates to UTF-8:

require 'iconv'
class ::String
  # Return a new String that has been transliterated into UTF-8
  # Should work in Ruby 1.8 and Ruby 1.9 thanks to http://po-ru.com/diary/fixing-invalid-utf-8-in-ruby-revisited/
  def as_utf8(from_encoding = 'UTF-8')
    ::Iconv.conv('UTF-8//TRANSLIT', from_encoding, self + ' ')[0..-2]
  end
end

"foo".as_utf8 #=> "foo"
"foo".as_utf8('ISO-8859-1') #=> "foo"

Thanks JohnZ!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文