|
大家好
由于游戏开发的原因,希望从任意字符得到其对应的编码
简单来说表格里每个单元格都是一个字符,要计算每个单元格对应的编码
由于根本不了解字符编码的原理,所以只抄了一段可以中文和UNICODE转化的代码。
但这段代码无法识别韩文,日文,等非汉字字符。
请问应该如何解决我这问题?
以下是找到的代码
Public Function GetHzCode(oChar As String, Index As Byte) As String
Dim myArray() As Byte
Dim lngUniCode As Long
Dim strHex As String
Dim strGaoHex As String
Dim L As Integer
Dim R As Integer
On Error Resume Next
Select Case Index
Case 0 'GBK 十六进制值
myArray = StrConv(oChar, vbFromUnicode)
GetHzCode = Hex(CCur(myArray(0)) * 256 + myArray(1))
Case 1 'Surrogate(四字节)编码
GetHzCode = Encode(oChar)
Case 2 'UNICODE十六进制编码=HE
strHex = Encode(oChar)
strGaoHex = Left$(strHex, 4)
lngUniCode = Val("&H" & strGaoHex)
If lngUniCode < -9984 And lngUniCode > -10240 Then '''四字节汉字(Len=2,LenB=4)
lngUniCode = (Val("&H" & strGaoHex) - Val("&H" & "D840")) * (-64512) + 666969088
GetHzCode = Hex(lngUniCode + Val("&H" & strHex))
Else '''双字节汉字(Len=1,LenB=2)
GetHzCode = Hex(AscW(oChar))
End If
Case 3 '''区位码
strHex = Hex(Asc(oChar))
L = Format(CInt("&H" + Mid$(strHex, 1, 2)) - 160, "00")
R = Format(CInt("&H" + Mid$(strHex, 3, 2)) - 160, "00")
If L < 0 Or R < 0 Then
GetHzCode = "可能是繁体字不能转换为区位码!"
Else
GetHzCode = L & R
End If
Case 4 '''Unicode码
GetHzCode = CStr(AscW(oChar))
End Select
End Function
Private Function Encode(strEncode As String) As String
'取得十六进制的UNICODE
Dim i As Long
Dim chrTmp As String
Dim ByteLower As String
Dim ByteUpper As String
Dim strReturn As String
For i = 1 To Len(strEncode)
chrTmp$ = Mid(strEncode, i, 1)
ByteLower$ = Hex$(AscB(MidB$(chrTmp$, 1, 1)))
If Len(ByteLower$) = 1 Then ByteLower$ = "0" & ByteLower$
ByteUpper$ = Hex$(AscB(MidB$(chrTmp$, 2, 1)))
If Len(ByteUpper$) = 1 Then ByteUpper$ = "0" & ByteUpper$
strReturn$ = strReturn$ & ByteUpper$ & ByteLower$
Next
Encode = strReturn$
End Function
|
评分
-
1
查看全部评分
-
|