呜呜呜,对VB一窍不通,但是领导确要求在短时间完成串口助手的设计,有原来的做参考:),所以只好到处找资料了,要求用16进制显示,除了用二进制转换外还有没有更好办法呢?求救求救,快点!!!

解决方案 »

  1.   

    http://community.csdn.net/Expert/FAQ/FAQ_Index.asp?id=195169
      

  2.   

    '二进制 → 十六进制
    Function BinToHex(BinStr As String) As String
    Dim i As Long
    BinStr = String$((4 - Len(BinStr) Mod 4) Mod 4, "0") & BinStr
    For i = 0 To Len(BinStr) \ 4 - 1
        Select Case Mid$(BinStr, i * 4 + 1, 4)
            Case "0000": BinToHex = BinToHex & "0"
            Case "0001": BinToHex = BinToHex & "1"
            Case "0010": BinToHex = BinToHex & "2"
            Case "0011": BinToHex = BinToHex & "3"
            Case "0100": BinToHex = BinToHex & "4"
            Case "0101": BinToHex = BinToHex & "5"
            Case "0110": BinToHex = BinToHex & "6"
            Case "0111": BinToHex = BinToHex & "7"
            Case "1000": BinToHex = BinToHex & "8"
            Case "1001": BinToHex = BinToHex & "9"
            Case "1010": BinToHex = BinToHex & "A"
            Case "1011": BinToHex = BinToHex & "B"
            Case "1100": BinToHex = BinToHex & "C"
            Case "1101": BinToHex = BinToHex & "D"
            Case "1110": BinToHex = BinToHex & "E"
            Case "1111": BinToHex = BinToHex & "F"
        End Select
    Next i
    End Function
      

  3.   

    Function BinToHex(BinStr As String) As String
    Dim i As Long, intStrlen As Integer
    Dim lngSum As Long    intStrlen = Len(BinStr)
        
        For i = 1 To intStrlen
            lngSum = lngSum + IIf(Mid(BinStr, i, 1) = 1, 2 ^ (intStrlen - i), 0)
        Next i
        
        BinToHex = CStr(Hex(lngSum))
        
    End Function
      

  4.   

    Function BinToHex(BinStr As String) As String
    Dim i As Long, intStrlen As Integer
    Dim lngSum As Long    intStrlen = Len(BinStr)
        
        For i = 1 To intStrlen
            lngSum = lngSum + IIf(Mid(BinStr, i, 1) = 1, 2 ^ (intStrlen - i), 0)
        Next i
        
        BinToHex = CStr(Hex(lngSum))
        
    End Function
      

  5.   

    速度不快你找我算帐。:)
    Private Sub Command1_Click()
      Dim tText As String
      Dim tOutText As String
      
      tText = BytesFile_Load("TECHINFO.TXT") '读入一个75KB的文本文件。
      tOutText = HexStrGetByBinStr(tText)
      'Text1.Text = HexStrGetByBinStr(tText)
    End SubFunction HexStrGetByBinStr(ByVal pBinStr As String) As String
      '将二进制字符串转换为十六进制
      Dim tOutStr As String                 '输出变量
      
      Dim tBytes() As Byte                  '输入字节表
      Dim tBytes_Length As Long             '输入字节表最大下标
      
      tBytes() = pBinStr
      tBytes_Length = UBound(tBytes())
        
      tBytes_Length = ((tBytes_Length + 1) \ 8) * 8 - 1
      
      If tBytes_Length < 0 Then Exit Function
      
      ReDim Preserve tBytes(tBytes_Length)
      
      Dim tAscValTable(0 To 255) As Byte    'Ascii映射表
      
      tAscValTable(49) = 1
      
      Dim tIndex As Long                    '总索引
      Dim tIndex_Start As Long              '总索引开始
      Dim tIndex_End As Long                '总索引结束
      
      tIndex_Start = 0
      tIndex_End = tBytes_Length
      
      Dim tOutBytes() As Byte               '输出字节表
      Dim tOutBytes_Length As Long          '输出字节表最大下标
      Dim tOutBytes_Index As Long           '输出字节表索引
      
      tOutBytes_Length = ((tBytes_Length + 1) \ 8) - 1
      ReDim tOutBytes(tOutBytes_Length)
      
      Dim tByteIndex As Long
      Dim tByteIndex_Start As Long
      Dim tByteIndex_End As Long
      
      Dim tAsc As Byte                      '字节Ascii
      Dim tAscVal As Byte                   '字节Ascii的映射值
      
      Dim tHexVal As Byte                   '
      Dim tHexVal_High As Boolean           '
        
      For tIndex = tIndex_Start To tIndex_End Step 8
        
        tHexVal = 0
        tByteIndex_Start = tIndex
        tByteIndex_End = tIndex + 7
        
        For tByteIndex = tByteIndex_Start To tByteIndex_End Step 2
          tAsc = tBytes(tByteIndex)
          tAscVal = tAscValTable(tAsc)
          tHexVal = tHexVal * 2 + tAscVal
        Next
        
        tOutBytes_Index = tIndex \ 8
        tHexVal_High = tHexVal > 9
        
        tOutBytes(tOutBytes_Index) = tHexVal + 48 + (tHexVal_High And 7)
      Next
      
      tOutStr = StrConv(tOutBytes(), vbUnicode)
      
      HexStrGetByBinStr = tOutStr
    End Function
      

  6.   

    多谢各位:)还有一个小问题,要求:用16进制发送和16进制显示;1、我看一些资料说传输数据的时候只有二进制和字符形式,那么我现在发送16进制有必要将其转换为二进制吗?还是VB本身就自动完成转换工作呢?2、同样在接收数据的时候,VB会自动将收到的数据转化成二进制吗?大家不要笑我很菜哈:)
      

  7.   

    没有必要转换,通过COM和外部设备交换数据,一般都是取决于外部设备所使用的传输形式。并且一般如PLC和数字示波器等设备都是以字符方式传送数据的,在你接受数据的时候接受到的也是文本格式。VB不会自动帮你作任何格式转换。你需要先要把文本格式的数据转换为数字,然后才能进行处理。
    而当你发送数据的时候也就只要直接把数据以文本格式发送就可以了。
      

  8.   

    你这里有一个误解。
    实际上,通过 COM 传送的数据都是以字节为单位,0-255(每 bit 当然是 0 或 1)。不存在所谓 16 进制。
    问题的要点在于发送和接收方使用相同的方法来解释数据。例如 30h,按 2 进制解释就是 10 进制的 48,按字符解释就是字符 "0"。记住,所谓 16 进制在计算机物理系统中并不存在。它只是一种便于人来读写的表达方式。在 VB 中,16进制是指字符串。例如 Hex(48)="30";Val("&H30")=48。
      

  9.   

    各位大侠,下面是我现在做参考的源码的一部分,这部分主要完成的功能是读模块名、读配置。它主要是以ASCII形式的,要以16进制应该怎么定义呢?目前很茫然。
    Option Explicit
    Dim i As Integer
    Dim sea As Boolean
    Dim str As String
    Dim strS As StringPrivate Sub Timer1_Timer()
       Dim ys As Single
       Dim NAME As String
       Label3 = i
       str = "$" + Right("0" + Hex(i), 2) + "M" + Chr(13)
       ys = Timer + 0.2
       frmComm.MSComm1.Output = str
       Do
          DoEvents
          If frmComm.MSComm1.InBufferCount >= 9 Then
             Exit Do
          End If
       Loop Until Timer > ys
       strS = frmComm.MSComm1.Input
       If Left(strS, 1) = "!" And Right(strS, 1) = Chr(13) Then
          NAME = Mid(strS, 4, 4)
          str = "$" + Right("0" + Hex(i), 2) + "2" + Chr(13)
          frmComm.MSComm1.Output = str
          ys = Timer + 0.2
          Do
            DoEvents
            If frmComm.MSComm1.InBufferCount >= 10 Then
               Exit Do
            End If
          Loop Until Timer > ys
          strS = frmComm.MSComm1.Input
          If Left(strS, 1) = "!" And Right(strS, 1) = Chr(13) Then
             Set lvitem = frmComm.LV.ListItems.Add(, , Format(i, "000") + "(" + Right("0" + Hex(i), 2) + "H)")
             lvitem.SubItems(1) = NAME
             lvitem.SubItems(4) = "Analog Input"
             lvitem.SubItems(6) = "% of Full"
             sea = True
          End If
       End If
       i = i + 1
       If i = sum + 1 Then
          Unload Me
          If sea = False Then
             MsgBox "没有找到模块,请查明原因!", vbOKOnly + vbExclamation, "搜索模块失败!"
          End If
       End If
    End Sub