首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VB >

关于文本和16进制互转的小疑点.

2012-02-15 
关于文本和16进制互转的小问题...先上代码:文本转16进制Public Function StrtoHex(ByVal strs As String)

关于文本和16进制互转的小问题...
先上代码:
'文本转16进制
Public Function StrtoHex(ByVal strs As String) As String 'str to 16
  Dim abytS() As Byte
  Dim bytTemp As Byte
  Dim strTemp As String
  Dim lLocation As Long
  abytS = StrConv(strs, vbFromUnicode)
  For lLocation = 0 To UBound(abytS)
  bytTemp = abytS(lLocation)
  strTemp = Hex(bytTemp)
  strTemp = Right("00" & strTemp, 2)
  StrtoHex = StrtoHex & strTemp
  Next lLocation
End Function

'16进制转文本
Public Function HextoStr(ByVal strs As String) As String '16 to str
  Dim i As Integer, tmp As String
  If Len(strs) Mod 2 Then Exit Function
  For i = 1 To Len(strs) Step 2
  n = Val("&H" & Mid(strs, i, 2))
  If n < 0 Or n > 127 Then
  n = Val("&H" & Mid(strs, i, 4))
  i = i + 2
  End If
  tmp = tmp & Chr(n)
  Next i
  HextoStr = tmp
End Function

使用这两个方法转换16进制和文本时,
比如我要把555转换成16进制,16进制栏里就是353535
怎么让它转换的结果变成35 35 35或35|35|35 ?
怎么弄?16进制转文本时也要这样的.

[解决办法]
你那办法我在初中时候用过,我教你一个高中时候用的办法:

VB code
Private Sub Command1_Click()  Text1.Text = StringPutToHEX("555")  Text2.Text = StringGetByHEX("D0 A1 CF C9 C3 C3 CA C7 B8 F6 BA C3 BA A2 D7 D3")End SubFunction StringPutToHEX(ByRef pString As String, Optional pLineWidth As Long = 16, Optional ByVal pLimit As String = " ") As String  Dim tSurBytes() As Byte, tSurBytes_Index As Long  Dim tDesBytes() As Byte, tDesBytes_Index As Long, tDesBytes_Length As Long  Dim tLimitCode As Byte    tSurBytes() = StrConv(pString, vbFromUnicode)  tLimitCode = Asc(pLimit)    tDesBytes_Length = UBound(tSurBytes()) * 3 + 2    ReDim tDesBytes(tDesBytes_Length)    For tDesBytes_Index = 0 To tDesBytes_Length Step 3    tDesBytes(tDesBytes_Index) = HexEnCode(tSurBytes(tSurBytes_Index) \ 16)    tDesBytes(tDesBytes_Index + 1) = HexEnCode(tSurBytes(tSurBytes_Index) Mod 16)    tDesBytes(tDesBytes_Index + 2) = tLimitCode    tSurBytes_Index = tSurBytes_Index + 1  Next    StringPutToHEX = StrConv(tDesBytes, vbUnicode)End FunctionFunction StringGetByHEX(ByRef pHEX As String) As String  Dim tSurBytes() As Byte, tSurBytes_Index As Long  Dim tDesBytes() As Byte, tDesBytes_Index As Long, tDesBytes_Length As Long  tSurBytes() = StrConv(pHEX, vbFromUnicode)    tDesBytes_Length = UBound(tSurBytes()) \ 3    ReDim tDesBytes(tDesBytes_Length)    For tDesBytes_Index = 0 To tDesBytes_Length    tDesBytes(tDesBytes_Index) = HexDeCode(tSurBytes(tSurBytes_Index)) * 16 + HexDeCode(tSurBytes(tSurBytes_Index + 1))    tSurBytes_Index = tSurBytes_Index + 3  Next    StringGetByHEX = StrConv(tDesBytes, vbUnicode)End FunctionFunction HexEnCode(pHEX As Byte) As Byte  HexEnCode = 48 + pHEX + ((pHEX > 9) And 7)End FunctionFunction HexDeCode(pHEX As Byte) As Byte  HexDeCode = pHEX - (48 + ((pHEX > 64) And 7))End Function
[解决办法]
VB code
'文本转16进制Public Function StrtoHex(ByVal strs As String) As String 'str to 16  Dim abytS() As Byte  Dim bytTemp As Byte  Dim strTemp As String  Dim lLocation As Long  abytS = StrConv(strs, vbFromUnicode)  For lLocation = 0 To UBound(abytS)  bytTemp = abytS(lLocation)  strTemp = Hex(bytTemp)  strTemp = Right("00" & strTemp, 2)  StrtoHex = StrtoHex & strTemp & " "  Next lLocation  StrtoHex = RTrim(StrtoHex)  End Function'16进制转文本Public Function HextoStr(ByVal strs As String) As String '16 to str  Dim i As Integer, tmp As String  If Len(strs) Mod 2 Then Exit Function  For i = 1 To Len(strs) Step 2  n = Val("&H" & Mid(strs, i, 2))  If n < 0 Or n > 127 Then  n = Val("&H" & Mid(strs, i, 4))  i = i + 2  End If  tmp = tmp & Chr(n) & " "  Next i  HextoStr = RTrim(tmp)End Function 

热点排行