String 在Visual Basic中将字节转换为字符串
我是VB新手,我正在尝试用一个程序来输出文本而不是十六进制。我在网上找到了代码,一个叫做maxiCOM的程序。这里是链接。源代码可以在页面底部下载。String 在Visual Basic中将字节转换为字符串,string,vb.net,hex,String,Vb.net,Hex,我是VB新手,我正在尝试用一个程序来输出文本而不是十六进制。我在网上找到了代码,一个叫做maxiCOM的程序。这里是链接。源代码可以在页面底部下载。 不幸的是,程序中的编码水平远远超出了我的理解水平,我真的不知道如何将输出从十六进制改为文本。如果有人能告诉我怎么做,我将不胜感激。代码片段是 Public Class MaxiTester Dim SpaceCount As Byte = 0 Dim LookUpTable As String = "0123456789ABCDEF" Dim
不幸的是,程序中的编码水平远远超出了我的理解水平,我真的不知道如何将输出从十六进制改为文本。如果有人能告诉我怎么做,我将不胜感激。代码片段是
Public Class MaxiTester
Dim SpaceCount As Byte = 0
Dim LookUpTable As String = "0123456789ABCDEF"
Dim RXArray(2047) As Char ' Text buffer. Must be global to be accessible from more threads.
Dim RXCnt As Integer ' Length of text buffer. Must be global too.
' Make a new System.IO.Ports.SerialPort instance, which is able to fire events.
Dim WithEvents COMPort As New SerialPort
Private Sub Receiver(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs) Handles COMPort.DataReceived
Dim RXByte As Byte
Do
'----- Start of communication protocol handling -----------------------------------------------------------
' The code between the two lines does the communication protocol. In this case, it simply emties the
' receive buffer and converts it to text, but for all practical applications, you must replace this part
' with a code, which can collect one entire telegram by searching for the telegram
' delimiter/termination. In case of a simple ASCII protocol, you may just use ReadLine and receive
' in a global string instead of a byte array.
' Because this routine runs on a thread pool thread, it does not block the UI, so if you have any data
' convertion, encryption, expansion, error detection, error correction etc. to do, do it here.
RXCnt = 0
Do
RXByte = COMPort.ReadByte
RXArray(RXCnt) = LookUpTable(RXByte >> 4) ' Convert each byte to two hexadecimal characters
RXCnt = RXCnt + 1
RXArray(RXCnt) = LookUpTable(RXByte And 15)
RXCnt = RXCnt + 1
RXArray(RXCnt) = " "
RXCnt = RXCnt + 1
SpaceCount = (SpaceCount + 1) And 31 ' Insert spaces and CRLF for better readability
If SpaceCount = 0 Then ' Insert CRLF after 32 numbers
RXArray(RXCnt) = Chr(13) ' CR
RXCnt = RXCnt + 1
RXArray(RXCnt) = Chr(10) ' LF
RXCnt = RXCnt + 1
Else
If (SpaceCount And 3) = 0 Then ' Insert two extra spaces for each 4 numbers
RXArray(RXCnt) = " "
RXCnt = RXCnt + 1
RXArray(RXCnt) = " "
RXCnt = RXCnt + 1
End If
End If
Loop Until (COMPort.BytesToRead = 0)
'----- End of communication protocol handling -------------------------------------------------------------
Me.Invoke(New MethodInvoker(AddressOf Display)) ' Start "Display" on the UI thread
Loop Until (COMPort.BytesToRead = 0) ' Don't return if more bytes have become available in the meantime
End Sub
' Text display routine, which appends the received string to any text in the Received TextBox.
Private Sub Display()
Received.AppendText(New String(RXArray, 0, RXCnt))
End Sub
请注意内部循环中
Do
之后的第一行:
RXByte = COMPort.ReadByte
这是代码中唯一从COM端口读取实际字节值的点。在此之后,代码使用位移位将字节转换为两个十六进制值。您应该创建一个string
类型的全局变量,并在该字符串丢失之前将读取的字节值追加到此字符串
在上一行之后立即添加以下行:
YourString &= Convert.ToChar(RXByte).ToString()
其中YourString
是全局string
变量
请注意,通过使用
StringBuilder
而不是string
,您可以获得一些效率,但我将这留给您作为练习。注意内部循环中Do
之后的第一行:
RXByte = COMPort.ReadByte
这是代码中唯一从COM端口读取实际字节值的点。在此之后,代码使用位移位将字节转换为两个十六进制值。您应该创建一个string
类型的全局变量,并在该字符串丢失之前将读取的字节值追加到此字符串
在上一行之后立即添加以下行:
YourString &= Convert.ToChar(RXByte).ToString()
其中YourString
是全局string
变量
请注意,通过使用
StringBuilder
而不是string
,您可以获得一些效率,但我将把它留给您作为练习。您应该解释一下这一行代码如何适合OP的情况。@dotNET很可能他们没有阅读问题,只是阅读标题。这是Microsoft推荐的方式;可能与问题无关,但总体上仍然相关。您应该解释一下这一行如何适合OP的情况。@dotNET很可能没有阅读问题,只是标题。这是Microsoft建议的投票方式;可能与问题无关,但总体上仍然相关谢谢您的帮助!在遵循您的说明并修改了一些显示例程之后,我让它开始工作。谢谢您的帮助!我得到它的工作后,按照您的指示,也修改了一些显示例行程序。