class String
def utf8_to_gb2312
encode_convert(self, "gb2312", "UTF-8")
end
private
def encode_convert(s, to, from)
require 'iconv'
begin
converter = Iconv.new(to, from)
converter.iconv(s)
rescue
s
end
end
end
s = "搞笑".utf8_to_gb2312
require 'uri'
puts URI.escape(s)
"http://index.baidu.com/main/word.php?word=%B8%E3%D0%A6"
本文提供了一个使用Ruby语言将UTF-8编码转换为GB2312编码的具体实现方法,并展示了如何利用Iconv库进行字符集转换。此外,还介绍了如何对转换后的字符串进行URL编码。
458

被折叠的 条评论
为什么被折叠?



