ITeye博客

ruby 转化ANSI 编为UTF-8

pobing · 2012-06-14 14:13 · 编程语言 · Markdown

用到:gem: rchardet19,string.encoding,string.force_encoding


require 'rchardet19' 


def convert_encode file
    string=File.open(file.location,"r+").read # 
    cd = CharDet.detect(string) 
   # 获取文件编码
   #cd.encoding
    if string.encoding != 'UTF-8' && cd.encoding=="utf-8"
      string.force_encoding('UTF-8') 
    elsif string.encoding != 'UTF-8'
      string.force_encoding('GBK') 
    end
   File.open(file.location, "w") { |f| f.puts string.encode("UTF-8") }
  end

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。