`

ruby 转化ANSI 编为UTF-8

阅读更多
用到: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
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics