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
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进制
'得到汉字
进制相互转换代码 :http://www.tiancao.net/bbs/ShowPost.asp?ThreadID=7
[本日志由 田草 于 2008-01-18 05:15 PM 编辑]
|
暂时没有评论
发表评论 - 不要忘了输入验证码哦! |