您好,今天小天来为大家解答以上的问题。汉字转unicode相信很多小伙伴还不知道,现在让我们一起来看看吧!

1、i = i + 1Option ExplicitPublic Function Unicode8Decode(bTemp() As Byte) As String'解码UNICODE UTF-8Dim i As LongDim k As LongDim strReturn As StringDim strTmp() As ByteDim Code1 As LongDim Code2 As LongDim Code4 As Longk = UBound(bTemp)ReDim strTmp(k 2)For i = 0 To kIf (bTemp(i) And 128) = 0 ThenstrTmp(bNo) = bTemp(i)bNo = bNo + 1ElseIf (bTemp(i) And 252) = 252 Then '11111100Code1 = (bTemp(i) And 1) 64 + bTemp(i + 1) And 63Code2 = (bTemp(i + 2) And 63) 4 + (bTemp(i + 3) And 48) 16Code3 = (bTemp(i + 3) And 15) 16 + (bTemp(i + 4) And 60) 4Code4 = (bTemp(i + 4) And 3) 64 + (bTemp(i + 5) And 63)Code = CLng("&H" + Hex(AscW(StrConv(ChrW(Code), vbFromUnicode))))i = i + 5strTmp(bNo) = Code And 255strTmp(bNo + 1) = Code 256strTmp(bNo + 1) = Code 65536strTmp(bNo + 1) = Code 16777216ElseIf (bTemp(i) And 248) = 248 Then '11111000Code1 = (bTemp(i) And 3)Code2 = (bTemp(i + 1) And 63) 4 + (bTemp(i + 2) And 48) 16Code3 = (bTemp(i + 2) And 15) 16 + (bTemp(i + 3) And 60) 4Code4 = (bTemp(i + 3) And 3) 64 + (bTemp(i + 4) And 63)Code = CLng("&H" + Hex(AscW(StrConv(ChrW(Code), vbFromUnicode))))i = i + 4strTmp(bNo) = Code And 255strTmp(bNo + 1) = Code 256strTmp(bNo + 1) = Code 65536strTmp(bNo + 1) = Code 16777216ElseIf (bTemp(i) And 240) = 240 Then '11110000Code2 = (bTemp(i + 1) And 15) 16 + (bTemp(i + 2) And 60) 4Code3 = (bTemp(i + 2) And 3) 64 + (bTemp(i + 3) And 63)Code = (Code1 256 + Code2) 256 + Code3Code = CLng("&H" + Hex(AscW(StrConv(ChrW(Code), vbFromUnicode))))i = i + 3strTmp(bNo) = Code And 255strTmp(bNo + 1) = Code 256strTmp(bNo + 1) = Code 65536strTmp(bNo + 1) = Code 16777216Exit DoElseIf (bTemp(i) And 224) = 224 Then '11100000Code1 = (bTemp(i) And 15) 16 + (bTemp(i + 1) And 60) 4Code2 = (bTemp(i + 1) And 3) 64 + (bTemp(i + 2) And 63)Code = Code1 256 + Code2Code = CLng("&H" + Hex(AscW(StrConv(ChrW(Code), vbFromUnicode))))i = i + 2strTmp(bNo) = Code And 255strTmp(bNo + 1) = Code 256bNo = bNo + 2ElseIf (bTemp(i) And 192) = 192 Then '11000000Code1 = (bTemp(i) And 28) 4Code2 = (bTemp(i) And 3) 64 + (bTemp(i + 1) And 63)Code = Code1 256 + Code2Code = CLng("&H" + Hex(AscW(StrConv(ChrW(Code), vbFromUnicode))))strTmp(bNo) = Code And 255strTmp(bNo + 1) = Code 256bNo = bNo + 2End IfReDim Preserve strTmp(bNo - 1)Unicode8Decode = strReturnPublic Function Unicode8Encode(bTemp As String) As Byte()'编码UNICODE UTF-8Dim i As LongDim j As LongDim k As LongDim strTotal() As ByteDim strTmp As StringDim Code1 As LongDim Code2 As LongDim Code4 As LongDim Code5 As LongDim Code6 As Long'已生成的字节数k = Len(bTemp)ReDim strTotal(k 3)If Code strTotal(bNo) = CodebNo = bNo + 1If bNo > 422386 ThenDebug.Print CodeEnd IfElseIf Code Code1 = ((Code And 1984&) 32&) + 192&Code2 = (Code And 63&) + 128&strTotal(bNo + 1) = Code2bNo = bNo + 2ElseIf Code Code2 = ((Code And 4032&) 64&) + 128&Code3 = (Code And 63&) + 128&strTotal(bNo + 1) = Code2strTotal(bNo + 2) = Code3bNo = bNo + 3ElseIf Code Code1 = ((Code And 1835008) 262144) + 240&Code2 = ((Code And 258048) 4096&) + 128&Code3 = ((Code And 4032&) 64&) + 128&Code4 = (Code And 63&) + 128&strTotal(bNo + 1) = Code2strTotal(bNo + 2) = Code3strTotal(bNo + 3) = Code4ElseIf Code Code1 = ((Code And 50331648) 16777216) + 248&Code2 = ((Code And 16515072) 262144) + 128&Code3 = ((Code And 258048) 4096&) + 128&Code4 = ((Code And 4032&) 64&) + 128&Code5 = (Code And 63&) + 128&strTotal(bNo + 1) = Code2strTotal(bNo + 2) = Code3strTotal(bNo + 3) = Code4strTotal(bNo + 4) = Code5bNo = bNo + 5ElseCode1 = IIf(Code And 1073741824 = 1073741824, 253&, 252&)Code2 = ((Code And 1056964608) 16777216) + 128&Code3 = ((Code And 16515072) 262144) + 128&Code4 = ((Code And 258048) 4096&) + 128&Code5 = ((Code And 4032&) 64&) + 128&Code6 = (Code And 63&) + 128&strTotal(bNo + 1) = Code2strTotal(bNo + 2) = Code3strTotal(bNo + 3) = Code4strTotal(bNo + 4) = Code5strTotal(bNo + 5) = Code6bNo = bNo + 6End IfReDim Preserve strTotal(bNo - 1)Unicode8Encode = strTotalPublic Function UnicodeDecode(bTemp() As Byte, Optional BigEndian As Boolean = False) As String'解码UNICODE UTF-16Dim i As LongDim strTotal() As ByteDim strReturn As StringDim Code1 As LongDim Code2 As LongReDim strTotal(UBound(bTemp))If BigEndian ThenFor i = LBound(bTemp) To UBound(bTemp) Step 2Code1 = bTemp(i)Code2 = bTemp(i + 1)Code = Code1 256 + Code2If Code > 255 ThenCode = CLng("&H" + Hex(AscW(StrConv(ChrW(Code), vbFromUnicode))))strTotal(bNo) = Code And 255strTotDim Code As Longal(bNo + 1) = Code 256bNo = bNo + 2ElsestrTotal(bNo) = CodebNo = bNo + 1End IfElseFor i = LBound(bTemp) To UBound(bTemp) Step 2Code1 = bTemp(i)Code2 = bTemp(i + 1)If Code > 255 ThenCode = CLng("&H" + Hex(AscW(StrConv(ChrW(Code), vbFromUnicode))))strTotal(bNo) = Code And 255strTotal(bNo + 1) = Code 256bNo = bNo + 2ElsestrTotal(bNo) = CodebNo = bNo + 1End IfEnd IfReDim Preserve strTotal(bNo - 1)strReturn = StrConv(strTotal, vbUnicode)UnicodeDecode = strReturnPublic Function UnicodeEncode(bTemp As String, Optional BigEndian As Boolean = False) As Byte()'编码UNICODE UTF-16Dim i As LongDim k As LongDim strTotal() As Bytek = Len(bTemp)ReDim strTotal(k 2)If BigEndian ThenstrTotal(bNo) = Code 256strTotal(bNo + 1) = Code And 255bNo = bNo + 2ElsestrTotal(bNo) = Code And 255strTotal(bNo + 1) = Code 256bNo = bNo + 2End IfReDim Preserve strTotal(bNo - 1)UnicodeEncode = strTotal。

本文到这结束,希望上面文章对大家有所帮助。