#RM可用的Iconv转码,因为经常用到所以来这做个备份了
module Iconv
CP_ACP = 0
CP_UTF8 = 65001
def self.utf8_to_gb2312(text)
m2w = Win32API.new('kernel32', 'MultiByteToWideChar', 'ilpipi', 'i')
w2m = Win32API.new('kernel32', 'WideCharToMultiByte', 'ilpipipp', 'i')
n = 0
i = m2w.call(CP_UTF8, 0 , text, -1,nil,0)
buffer = "\0" * (i*2)
m2w.call(CP_UTF8, 0 , text, -1, buffer, i)
i = w2m.call(CP_ACP, 0, buffer, -1, nil, 0, nil, nil)
result = "\0" * i
w2m.call(CP_ACP, 0, buffer, -1, result, i, nil, nil)
result.chop!
return result
end
def self.gb2312_to_utf8(text)
m2w = Win32API.new('kernel32', 'MultiByteToWideChar', 'ilpipi', 'i')
w2m = Win32API.new('kernel32', 'WideCharToMultiByte', 'ilpipipp', 'i')
n = text.size
i = m2w.call(CP_ACP, 0, text, n+1, nil, 0)
buffer = "\0" * (i*2)
m2w.call(CP_ACP, 0, text, n+1, buffer, i)
i = w2m.call(CP_UTF8, 0, buffer, -1, nil, 0, nil, nil)
result = "\0" * i
w2m.call(CP_UTF8, 0, buffer, -1, result, i, nil, nil)
result.chop!
return result
end
end