赞 | 123 |
VIP | 13 |
好人卡 | 16 |
积分 | 194 |
经验 | 38692 |
最后登录 | 2024-11-15 |
在线时间 | 3102 小时 |
Lv4.逐梦者
- 梦石
- 0
- 星屑
- 19423
- 在线时间
- 3102 小时
- 注册时间
- 2013-1-11
- 帖子
- 1291
|
- class String
- CP_ACP = 0
- CP_UTF8 = 65001
- def utf8_to_gb2312
- m2w = Win32API.new('kernel32', 'MultiByteToWideChar', 'ilpipi', 'i')
- w2m = Win32API.new('kernel32', 'WideCharToMultiByte', 'ilpipipp', 'i')
- n = 0
- i = m2w.call(CP_UTF8, 0 , self, -1, nil, 0)
- buffer = "\0" * (i*2)
- m2w.call(CP_UTF8, 0 , self, -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 gb2312_to_utf8
- m2w = Win32API.new('kernel32', 'MultiByteToWideChar', 'ilpipi', 'i')
- w2m = Win32API.new('kernel32', 'WideCharToMultiByte', 'ilpipipp', 'i')
- n = self.size
- i = m2w.call(CP_ACP, 0, self, n+1, nil, 0)
- buffer = "\0" * (i*2)
- m2w.call(CP_ACP, 0, self, 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
复制代码
Dir.getwd.gb2312_to_utf8 # 试试
|
评分
-
查看全部评分
|