加入我们,或者,欢迎回来。
您需要 登录 才可以下载或查看,没有帐号?注册会员
x
本帖最后由 RPGzh500223 于 2022-1-23 20:40 编辑
参考资料:libconv
class String ICONV = "iconv.dll" Iconv_open = Win32API.new(ICONV, 'libiconv_open', 'PP', 'L') Iconv_close = Win32API.new(ICONV, 'libiconv_close', 'L', 'L') Iconv_do = Win32API.new(ICONV, 'libiconv', 'LPPPP', 'L') def convert(dst_charset, src_charset = 'UTF-8') dst_str = "\0" * (self.size << 1) iconv_t = Iconv_open.call(dst_charset, src_charset) return if iconv_t == -1 inbuf = [self].pack('p') inbytesleft = [self.size].pack('L') outbuf = [dst_str].pack('p') outbytesleft = [dst_str.size].pack('L') Iconv_do.call(iconv_t, inbuf, inbytesleft, outbuf, outbytesleft) Iconv_close.call(iconv_t) return dst_str[0, dst_str.size - outbytesleft.unpack('L')[0]] end CallWndProc = Win32API.new('user32', 'CallWindowProc', 'PLLLL', 'L') GetProcAddress = Win32API.new('kernel32', 'GetProcAddress', 'LP', 'L') LoadLibrary = Win32API.new('kernel32', 'LoadLibrary', 'P', 'L') HICONV = LoadLibrary.call(ICONV) ProcAddr = [GetProcAddress.call(HICONV, 'libiconv_open'), GetProcAddress.call(HICONV, 'libiconv'), GetProcAddress.call(HICONV, 'libiconv_close')].pack('L3') CodeIconv = [85,139,236,139,69,8,139,28,69,16,0,0,0,139,83,4,255,114,12, 139,19,255,114,12,139,69,20,139,28,69,12,0,0,0,62,137,93,20,255,19,131, 196,8,131,248,255,116,110,62,137,69,8,139,125,12,209,231,131,199,8,255, 55,255,119,4,139,117,16,209,230,131,198,8,255,54,255,118,4,87,131,199, 4,87,86,131,198,4,86,62,255,117,8,139,93,20,255,83,4,131,196,20,139,93, 16,143,4,93,12,0,0,0,143,4,93,8,0,0,0,139,93,12,143,4,93,12,0,0,0,88, 43,4,93,8,0,0,0,137,4,93,8,0,0,0,80,62,255,117,8,139,93,20,255,83,8, 131,196,4,88,201,194,16,0].pack('C*') def convert2(dst_charset, src_charset = 'UTF-8') dst_str = "\0" * (self.size << 1) bytes = CallWndProc.call(CodeIconv, [dst_charset, src_charset].object_id, dst_str.object_id, self.object_id, ProcAddr.object_id) dst_str end end
class String
ICONV = "iconv.dll"
Iconv_open = Win32API.new(ICONV, 'libiconv_open', 'PP', 'L')
Iconv_close = Win32API.new(ICONV, 'libiconv_close', 'L', 'L')
Iconv_do = Win32API.new(ICONV, 'libiconv', 'LPPPP', 'L')
def convert(dst_charset, src_charset = 'UTF-8')
dst_str = "\0" * (self.size << 1)
iconv_t = Iconv_open.call(dst_charset, src_charset)
return if iconv_t == -1
inbuf = [self].pack('p')
inbytesleft = [self.size].pack('L')
outbuf = [dst_str].pack('p')
outbytesleft = [dst_str.size].pack('L')
Iconv_do.call(iconv_t, inbuf, inbytesleft, outbuf, outbytesleft)
Iconv_close.call(iconv_t)
return dst_str[0, dst_str.size - outbytesleft.unpack('L')[0]]
end
CallWndProc = Win32API.new('user32', 'CallWindowProc', 'PLLLL', 'L')
GetProcAddress = Win32API.new('kernel32', 'GetProcAddress', 'LP', 'L')
LoadLibrary = Win32API.new('kernel32', 'LoadLibrary', 'P', 'L')
HICONV = LoadLibrary.call(ICONV)
ProcAddr = [GetProcAddress.call(HICONV, 'libiconv_open'),
GetProcAddress.call(HICONV, 'libiconv'),
GetProcAddress.call(HICONV, 'libiconv_close')].pack('L3')
CodeIconv = [85,139,236,139,69,8,139,28,69,16,0,0,0,139,83,4,255,114,12,
139,19,255,114,12,139,69,20,139,28,69,12,0,0,0,62,137,93,20,255,19,131,
196,8,131,248,255,116,110,62,137,69,8,139,125,12,209,231,131,199,8,255,
55,255,119,4,139,117,16,209,230,131,198,8,255,54,255,118,4,87,131,199,
4,87,86,131,198,4,86,62,255,117,8,139,93,20,255,83,4,131,196,20,139,93,
16,143,4,93,12,0,0,0,143,4,93,8,0,0,0,139,93,12,143,4,93,12,0,0,0,88,
43,4,93,8,0,0,0,137,4,93,8,0,0,0,80,62,255,117,8,139,93,20,255,83,8,
131,196,4,88,201,194,16,0].pack('C*')
def convert2(dst_charset, src_charset = 'UTF-8')
dst_str = "\0" * (self.size << 1)
bytes = CallWndProc.call(CodeIconv, [dst_charset, src_charset].object_id,
dst_str.object_id, self.object_id, ProcAddr.object_id)
dst_str
end
end
String#convert2和String#convert都是调用DLL,前者效率稍高,至于DLL网上搜索下载即可
效率没有win32api快,但是支持的编码更多,具体可以看上面的链接
500字符的中文 UTF-8 转 GBK 测试 (具体次数好像和电脑性能相关,我的电脑 RMXP Time无执行代码测试 21.9-22.2W次/秒)
|