'二进制 → 十六进制 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
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
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
速度不快你找我算帐。:) 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 '输入字节表最大下标
各位大侠,下面是我现在做参考的源码的一部分,这部分主要完成的功能是读模块名、读配置。它主要是以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
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
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
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
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
而当你发送数据的时候也就只要直接把数据以文本格式发送就可以了。
实际上,通过 COM 传送的数据都是以字节为单位,0-255(每 bit 当然是 0 或 1)。不存在所谓 16 进制。
问题的要点在于发送和接收方使用相同的方法来解释数据。例如 30h,按 2 进制解释就是 10 进制的 48,按字符解释就是字符 "0"。记住,所谓 16 进制在计算机物理系统中并不存在。它只是一种便于人来读写的表达方式。在 VB 中,16进制是指字符串。例如 Hex(48)="30";Val("&H30")=48。
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