Chinaunix首页 | 论坛 | 博客
  • 博客访问: 181040
  • 博文数量: 14
  • 博客积分: 1403
  • 博客等级: 上尉
  • 技术积分: 192
  • 用 户 组: 普通用户
  • 注册时间: 2009-06-18 16:36
文章分类

全部博文(14)

文章存档

2011年(5)

2010年(9)

分类: WINDOWS

2010-05-18 09:57:14


#Include once "windows.bi"


''字符串转UTF16编码
Function StringToUTF16(sString As String) As String
    Dim As Long i, AL = Len(sString)
    Dim As String wide, oututf, h1, h2
    wide = Space(AL*2)
    Dim As Ubyte Ptr pSrc = StrPtr(wide)
    MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, StrPtr(sString), -1, Cast(LPWSTR,Strptr(wide)), AL)
    For i = 0 To Len(sString) * 2 Step 2
        h1 = Hex(pSrc[i+1])
        If Len(h1) < 2 Then h1 = "0" & h1
        h2 = Hex(pSrc[i])
        If Len(h2) < 2 Then h2 = "0" & h2
        If h1 & h2 = "0000" Then Exit For
        oututf &= h2 & h1
    Next
    Return oututf
End Function

''UTF16编码转字符串
Function UTF16ToString(sString As String) As String
    Dim As Long i, p = 0, AL = Len(sString)
    Dim As String wide, s2, h1, h2
    wide = Space(AL)
    s2 = Space(AL)
    Dim As Ubyte Ptr pSrc = StrPtr(wide)
    For i = 0 To Len(sString) Step 4
        h1 = Mid(sString, i+3, 2)
        If Mid(h1, 1, 1) = "0" Then h1 = Mid(h1, 2, 1)
        h2 = Mid(sString, i+1, 2)
        If Mid(h2, 1, 1) = "0" Then h2 = Mid(h2, 2, 1)
        pSrc[p+1] = Val("&h" & h1)
        pSrc[p] = Val("&h" & h2)
        p += 2
    Next
    WideCharToMultiByte(CP_OEMCP,NULL,StrPtr(wide),-1,Cast(LPWSTR,Strptr(s2)),Al,NULL,FALSE)
    Return s2
End Function

''用法示例
Dim As String uString,sString = "seesee越光宝盒"
uString = StringToUTF16(sString) '转换字符串sString为UTF16编码值
?uString
?UTF16ToString(uString)'UTF16编码值转换成字字符串


阅读(2254) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~