Created
November 20, 2011 15:18
-
-
Save john1king/1380350 to your computer and use it in GitHub Desktop.
to utf-8
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def txt_to_utf(file) | |
open(file) do |f| | |
s = f.read | |
#shift_jis 必须写在前面,因为被误判为gbk的概率很高 | |
x = %w(shift_jis gbk big5) | |
begin | |
u = s.dup.encode("utf-8",x.shift) | |
rescue Encoding::UndefinedConversionError,Encoding::InvalidByteSequenceError => ex | |
if x.empty? | |
if s.chr.ord.to_s(16) == "efbb" | |
#处理utf-8 with bom 文件 | |
end | |
else | |
retry | |
end | |
else | |
#处理 shift-jis, gbk, big5 文件 | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment