赞 | 1 |
VIP | 0 |
好人卡 | 1 |
积分 | 1 |
经验 | 5436 |
最后登录 | 2023-1-31 |
在线时间 | 585 小时 |
Lv1.梦旅人
- 梦石
- 0
- 星屑
- 68
- 在线时间
- 585 小时
- 注册时间
- 2013-5-25
- 帖子
- 1524
|
纯摘录
不止是调用API,在其它方面用中文路径也会出问题,尽量不要使用吧
#==============================================================================
# ■ String
#------------------------------------------------------------------------------
# String 类追加定义。
#==============================================================================
class String
#----------------------------------------------------------------------------
# ● API
#----------------------------------------------------------------------------
@@MultiByteToWideChar = Win32API.new('kernel32', 'MultiByteToWideChar', 'ilpipi', 'i')
@@WideCharToMultiByte = Win32API.new('kernel32', 'WideCharToMultiByte', 'ilpipipp', 'i')
#----------------------------------------------------------------------------
# ● UTF-8 转 Unicode
#----------------------------------------------------------------------------
def u2w
i = @@MultiByteToWideChar.call(65001, 0 , self, -1, nil,0)
buffer = "\0" * (i*2)
@@MultiByteToWideChar.call(65001, 0 , self, -1, buffer, i)
buffer.chop!
return buffer
end
#----------------------------------------------------------------------------
# ● UTF-8 转系统编码
#----------------------------------------------------------------------------
def u2s
i = @@MultiByteToWideChar.call(65001, 0 , self, -1, nil,0)
buffer = "\0" * (i*2)
@@MultiByteToWideChar.call(65001, 0 , self, -1, buffer, i)
i = @@WideCharToMultiByte.call(0, 0, buffer, -1, nil, 0, nil, nil)
result = "\0" * i
@@WideCharToMultiByte.call(0, 0, buffer, -1, result, i, nil, nil)
result.chop!
return result
end
#----------------------------------------------------------------------------
# ● 系统编码转 UTF-8
#----------------------------------------------------------------------------
def s2u
i = @@MultiByteToWideChar.call(0, 0, self, -1, nil, 0)
buffer = "\0" * (i*2)
@@MultiByteToWideChar.call(0, 0, self, -1, buffer, buffer.size / 2)
i = @@WideCharToMultiByte.call(65001, 0, buffer, -1, nil, 0, nil, nil)
result = "\0" * i
@@WideCharToMultiByte.call(65001, 0, buffer, -1, result, result.size, nil, nil)
result.chop!
return result
end
end |
|