Ruby 上的 Iconv 和 Kconv (1.9.2)
我知道 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如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
使用类似的东西了
然后你就可以开始放心地 。如果您有更复杂的需求,请阅读http://blog.grayproducts.net/categories/character_encodings
更新 感谢 JohnZ 的评论
Iconv
在 Ruby 1.9 中仍然有用,因为它可以音译字符(这是String#encode
等人无法做到的)。下面是如何使用音译为 UTF-8 的函数扩展String
的示例:谢谢 JohnZ!
As https://stackoverflow.com/users/23649/jtbandes says, it looks
Kconv
is likeIconv
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 needKconv
.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
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 thatString#encode
et al. can't do). Here's an example of how to extendString
with a function that transliterates to UTF-8:Thanks JohnZ!