先上代码:
'文本转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进制转文本时也要这样的.

解决方案 »

  1.   

    你那办法我在初中时候用过,我教你一个高中时候用的办法:
    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
      

  2.   


    '文本转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
      

  3.   

    chinaboyzyq你的方法虽然简单,但不行啊.
    555倒是能转换成35 35 35
    可是35 35 35转换文本时就有问题了,转换成5   5了.
    我再试试KiteGirl的办法.
      

  4.   

    OK了,KiteGirl你的办法成功了,谢谢二位了.