加载中…
正文 字体大小:

VB 汉字  字节 转换

(2009-07-07 08:15:59)
标签:

杂谈

分类: VB

VB 汉字编码 转换

 


'字节数值转汉字
Function Bytes_to_Unicode(Bytes, CodeType As String)
    Dim strReturn As String
    Dim i As Long
    Dim ThisCharCode As Integer
    Dim NextCharCode As Integer
    Dim ThirdCharCode As Integer
    strReturn = ""
    For i = 1 To LenB(Bytes)
        ThisCharCode = AscB(MidB(Bytes, i, 1))
        If ThisCharCode < &H80 Then
            strReturn = strReturn & Chr(ThisCharCode)
        Else
            If CodeType = "UTF-8" Or CodeType = "UTF8" Then
                NextCharCode = AscB(MidB(Bytes, i + 1, 1))
                ThirdCharCode = AscB(MidB(Bytes, i + 2, 1))
                strReturn = strReturn & UTF8_to_Unicode(ThisCharCode, NextCharCode, ThirdCharCode)
                i = i + 2
            Else
                NextCharCode = AscB(MidB(Bytes, i + 1, 1))
                strReturn = strReturn & Unicode(ThisCharCode, NextCharCode)
                i = i + 1
            End If
        End If
    Next
    Bytes_to_Unicode = strReturn
End Function
'二字节汉字转换
Function Unicode(BY1, BY2) As String
    Unicode = Chr(Int(BY1) * 256 + Int(BY2))
End Function
'三字节的UTF-8编码转二字节的Unicode编码
Function UTF8_to_Unicode(BY1, BY2, BY3) As String
    Dim BIN_UTF8 As String
    BIN_UTF8 = DEC_to_BIN(Int(BY1)) & DEC_to_BIN(Int(BY2)) & DEC_to_BIN(Int(BY3))
    Dim BIN_Unicode As String
    BIN_Unicode = Mid(BIN_UTF8, 5, 4) & Mid(BIN_UTF8, 11, 6) & Mid(BIN_UTF8, 19, 6)
    Dim DEC_Unicode As Long
    DEC_Unicode = BIN_to_DEC(BIN_Unicode)
    UTF8_to_Unicode = ChrW(DEC_Unicode)
End Function

 

双字节的转换,直接用第一个字节*256+第二个字节

'UTF-B的三字节二进制:1110xxxx,10xxxxxx,10xxxxxx
'Unicde的二字节对应的二进制是上面的x组合:xxxx+xxxxxx+xxxxxx
'将组合后的二进制转为10进制
'得到汉字

 

欢迎转载,本文转自[田草博客www.tiancao.net] 原文链接:http://tiancao.net/blogview.asp?logID=312

0

阅读 评论 收藏 转载 喜欢 打印举报
已投稿到:
  • 评论加载中,请稍候...
发评论

       

    发评论

    以上网友发言只代表其个人观点,不代表新浪网的观点或立场。

      

    新浪BLOG意见反馈留言板 不良信息反馈 电话:4006900000 提示音后按1键(按当地市话标准计费) 欢迎批评指正

    新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 会员注册 | 产品答疑

    新浪公司 版权所有