赞 | 13 |
VIP | 320 |
好人卡 | 64 |
积分 | 3 |
经验 | 112963 |
最后登录 | 2022-8-25 |
在线时间 | 2355 小时 |
Lv2.观梦者 (暗夜天使)
- 梦石
- 0
- 星屑
- 266
- 在线时间
- 2355 小时
- 注册时间
- 2009-3-13
- 帖子
- 2309
|
本帖最后由 Sion 于 2014-1-13 23:34 编辑
class String# 转码-- MultiByteToWideChar = Win32API.new('kernel32', 'MultiByteToWideChar', 'ilpipi', 'i') WideCharToMultiByte = Win32API.new('kernel32', 'WideCharToMultiByte', 'ilpipipp', 'i') CP_JIS = 932 CP_UTF8 = 65001 def j2u w2m(CP_UTF8, m2w(CP_JIS, self)).chop end # - def m2w(cpi, buf) nLen = MultiByteToWideChar.call(cpi, 0, buf, -1, nil, 0) * 2 wBuf = "\0" * nLen MultiByteToWideChar.call(cpi, 0, buf, -1, wBuf, nLen) return wBuf end def w2m(cpi, buf) nLen = WideCharToMultiByte.call(cpi, 0, buf, -1, nil, 0, nil, nil) * 2 wBuf = "\0" * nLen WideCharToMultiByte.call(cpi, 0, buf, -1, wBuf, nLen, nil, nil) return wBuf end end
class String# 转码--
MultiByteToWideChar = Win32API.new('kernel32', 'MultiByteToWideChar', 'ilpipi', 'i')
WideCharToMultiByte = Win32API.new('kernel32', 'WideCharToMultiByte', 'ilpipipp', 'i')
CP_JIS = 932
CP_UTF8 = 65001
def j2u
w2m(CP_UTF8, m2w(CP_JIS, self)).chop
end
# -
def m2w(cpi, buf)
nLen = MultiByteToWideChar.call(cpi, 0, buf, -1, nil, 0) * 2
wBuf = "\0" * nLen
MultiByteToWideChar.call(cpi, 0, buf, -1, wBuf, nLen)
return wBuf
end
def w2m(cpi, buf)
nLen = WideCharToMultiByte.call(cpi, 0, buf, -1, nil, 0, nil, nil) * 2
wBuf = "\0" * nLen
WideCharToMultiByte.call(cpi, 0, buf, -1, wBuf, nLen, nil, nil)
return wBuf
end
end
读取Shift-JIS编码的文件:
str = File.open(filename).read p str.j2u
str = File.open(filename).read
p str.j2u
|
评分
-
参与人数 1 | 星屑 +100 |
梦石 +1 |
收起
理由
|
迷糊的安安
| + 100 |
+ 1 |
认可答案 附赠66RPG提供的精美好人卡一张^^. |
查看全部评分
|