"汉字"->十进制数->十六进制数 Public Function ConverUnicodeName(NameStr As String) As Byte() Dim i As Integer Dim l As Long Dim temHex As String Dim ConName(19) As Byte temHex = " " For i = 0 To Len(NameStr) - 1 l = AscW(Mid(NameStr, i + 1, 1)) temHex = IIf(Len(Hex(l)) < 4, "0" & Hex(l), Hex(l)) temHex = Right(Hex(l), 4)
Do While Len(temHex) < 4 temHex = "0" & temHex Loop
ConName(2 + 2 * i) = HexToDec(Left(temHex, 2)) ConName(3 + 2 * i) = HexToDec(Right(temHex, 2)) Next End Function------- 十进制->十六进制 Public Function HexToDec(ByVal Value As String) As Long Dim i As Integer Dim returnValue As Long Dim num As IntegerFor i = Len(Value) To 1 Step -1 Select Case Mid(Value, i, 1) 'Mid(Len(Value) - i - 1)
Case "0" To "9" num = CInt(Mid(Value, i, 1)) Case "A" num = 10 Case "B" num = 11 Case "C" num = 12 Case "D" num = 13 Case "E" num = 14 Case "F" num = 15 End Select
returnValue = returnValue + num * (16 ^ (Len(Value) - i)) Next HexToDec = returnValue End Function
Hex(ascw("中")) '4E2D Unicode Chrw(val("&h4E2D")) '中 '----------------------------------------- Dim TmpCode() As Byte ReDim TmpCode(0 To 1) TmpCode = StrConv("中", vbFromUnicode) MsgBox ("Hex of ""中"":" & Hex(TmpCode(0)) & Hex(TmpCode(1)))Dim TmpCode() As Byte ReDim TmpCode(0 To 1) TmpCode(0) = &HA4& TmpCode(1) = &HA4& MsgBox (StrConv(TmpCode, vbUnicode))
不知楼上写的是什么功能的代码。另外写一个提供给楼主:Option ExplicitFunction GBToHex(sGB As String) As String Dim sResult As String Dim iLoop As Integer sResult = "" For iLoop = 1 To Len(sGB) sResult = sResult + Right("0000" + Hex(Asc(Mid(sGB, iLoop, 1))), 4) Next iLoop GBToHex = sResult End FunctionFunction HexToGB(sHex As String) As String Dim sResult As String Dim iLoop As Integer sResult = "" For iLoop = 1 To Len(sHex) \ 4 sResult = sResult + Chr(Val("&H" + Mid(sHex, iLoop * 4 - 3, 4))) Next iLoop HexToGB = sResult End FunctionPrivate Sub Form_Load() Dim sTemp As String sTemp = GBToHex("中国 China") Debug.Print sTemp sTemp = HexToGB(sTemp) Debug.Print sTemp End End Sub
Public Function ConverUnicodeName(NameStr As String) As Byte()
Dim i As Integer
Dim l As Long
Dim temHex As String
Dim ConName(19) As Byte temHex = " "
For i = 0 To Len(NameStr) - 1
l = AscW(Mid(NameStr, i + 1, 1))
temHex = IIf(Len(Hex(l)) < 4, "0" & Hex(l), Hex(l))
temHex = Right(Hex(l), 4)
Do While Len(temHex) < 4
temHex = "0" & temHex
Loop
ConName(2 + 2 * i) = HexToDec(Left(temHex, 2))
ConName(3 + 2 * i) = HexToDec(Right(temHex, 2))
Next
End Function-------
十进制->十六进制
Public Function HexToDec(ByVal Value As String) As Long
Dim i As Integer
Dim returnValue As Long
Dim num As IntegerFor i = Len(Value) To 1 Step -1 Select Case Mid(Value, i, 1) 'Mid(Len(Value) - i - 1)
Case "0" To "9"
num = CInt(Mid(Value, i, 1))
Case "A"
num = 10
Case "B"
num = 11
Case "C"
num = 12
Case "D"
num = 13
Case "E"
num = 14
Case "F"
num = 15
End Select
returnValue = returnValue + num * (16 ^ (Len(Value) - i))
Next
HexToDec = returnValue
End Function
Chrw(val("&h4E2D")) '中
'-----------------------------------------
Dim TmpCode() As Byte
ReDim TmpCode(0 To 1)
TmpCode = StrConv("中", vbFromUnicode)
MsgBox ("Hex of ""中"":" & Hex(TmpCode(0)) & Hex(TmpCode(1)))Dim TmpCode() As Byte
ReDim TmpCode(0 To 1)
TmpCode(0) = &HA4&
TmpCode(1) = &HA4&
MsgBox (StrConv(TmpCode, vbUnicode))
只要把"十六时制娄"->"十进制数"->"汉字"{ ChrW(long)}更正一下.
HexToDec这个函数为"十六进制"->"十进制"十进制->十六进制 用HEX函数就可以做到的.
Dim sResult As String
Dim iLoop As Integer
sResult = ""
For iLoop = 1 To Len(sGB)
sResult = sResult + Right("0000" + Hex(Asc(Mid(sGB, iLoop, 1))), 4)
Next iLoop
GBToHex = sResult
End FunctionFunction HexToGB(sHex As String) As String
Dim sResult As String
Dim iLoop As Integer
sResult = ""
For iLoop = 1 To Len(sHex) \ 4
sResult = sResult + Chr(Val("&H" + Mid(sHex, iLoop * 4 - 3, 4)))
Next iLoop
HexToGB = sResult
End FunctionPrivate Sub Form_Load()
Dim sTemp As String
sTemp = GBToHex("中国 China")
Debug.Print sTemp
sTemp = HexToGB(sTemp)
Debug.Print sTemp
End
End Sub
用的是unsigned(僵哥(当程序语言成为普及的第三语言之后……)) 的方法
很久没用VB了,一点感觉都没有了,哈
谢谢各位。人人给分!
呵呵,如果非汉字。你转换出来的还是2个字节。一个字节其实就够了。
当然意思到位了。