将字符串发送到它的十六进制等价物
Send the string to its hex equivalent
我想向我的设备发送 hex
命令,因为它只能理解 hex
。
因此,我设法创建了一个函数,可以验证 string
的用户输入是否具有 有效 对应的 hex
。问题是.
因此,通过验证 users input
是否具有对应的 hex
等价物,我确信我的系统发送的内容将被我的设备读取。 By searching 我意识到它需要转换为字节,它指出
Use the ASCIIEncoding class to convtert strings to an array of bytes
you can transmit.
Code:
Dim str as String = "12345678"
Dim bytes() as Byte = ASCIIEncoding.ASCII.GetBytes(strNumbers)
' Then Send te bytes to the reader
sp.Write(bytes, 0, bytes.Length)
You do not need to covert the values to HEX, in this case HEX is
mearly a different way of displaying the same thing.
我的代码:
'This is a string with corresponding hex value
Dim msg_cmd as string = "A0038204D7"
'Convert it to byte so my device can read it
Dim process_CMD() As Byte = ASCIIEncoding.ASCII.GetBytes(msg_cmd)
'Send it as bytes
ComPort.Write(process_CMD, 0, process_CMD.Length)
我的输出:
41 30 30 33 38 32 30 34 44 37
期望的输出:
A0 03 82 04 D7
要发送特定的字节序列,请不要发送字符串——只需发送字节:
Dim process_CMD() As Byte = { &HA0, &H03, &H82, &H04, &HD7 }
ComPort.Write(process_CMD, 0, process_CMD.Length)
正如我在上面的评论中提到的,这些值只是数值。十六进制没有什么特别的。十六进制只是表示相同值的另一种方式。换句话说,上面的代码与此完全相同:
Dim process_CMD() As Byte = { 160, 3, 130, 4, 215 }
ComPort.Write(process_CMD, 0, process_CMD.Length)
如果字符串中包含十六进制数字,则可以使用 Convert.ToByte
方法的 appropriate overload 将十六进制数字的字符串表示形式转换为字节值。但是,一次只能转换一个字节,因此,首先您需要将字符串拆分为字节(每个字节两个十六进制数字。例如:
Dim input As String = "A0038204D7"
Dim bytes As New List(Of Byte)()
For i As Integer = 0 to input.Length Step 2
bytes.Add(Convert.ToByte(input.SubString(i, 2), 16)
Next
Dim process_CMD() As Byte = bytes.ToArray()
ComPort.Write(process_CMD, 0, process_CMD.Length)
但是,如果字符串的字节之间有空格,会更容易。那么你可以只使用 String.Split
方法:
Dim input As String = "A0 03 82 04 D7"
Dim hexBytes As String() = input.Split(" "c)
Dim bytes As New List(Of Byte)()
For Each hexByte As String in hexBytes
bytes.Add(Convert.ToByte(hexByte, 16)
Next
Dim process_CMD() As Byte = bytes.ToArray()
ComPort.Write(process_CMD, 0, process_CMD.Length)
或者,更简单地说:
Dim input As String = "A0 03 82 04 D7"
Dim process_CMD() As Byte = input.Split(" "c).Select(Function(x) Convert.ToByte(x, 16)).ToArray()
ComPort.Write(process_CMD, 0, process_CMD.Length)
我想向我的设备发送 hex
命令,因为它只能理解 hex
。
因此,我设法创建了一个函数,可以验证 string
的用户输入是否具有 有效 对应的 hex
。问题是
因此,通过验证 users input
是否具有对应的 hex
等价物,我确信我的系统发送的内容将被我的设备读取。 By searching 我意识到它需要转换为字节,它指出
Use the ASCIIEncoding class to convtert strings to an array of bytes you can transmit.
Code:
Dim str as String = "12345678"
Dim bytes() as Byte = ASCIIEncoding.ASCII.GetBytes(strNumbers)
' Then Send te bytes to the reader
sp.Write(bytes, 0, bytes.Length)
You do not need to covert the values to HEX, in this case HEX is mearly a different way of displaying the same thing.
我的代码:
'This is a string with corresponding hex value
Dim msg_cmd as string = "A0038204D7"
'Convert it to byte so my device can read it
Dim process_CMD() As Byte = ASCIIEncoding.ASCII.GetBytes(msg_cmd)
'Send it as bytes
ComPort.Write(process_CMD, 0, process_CMD.Length)
我的输出:
41 30 30 33 38 32 30 34 44 37
期望的输出:
A0 03 82 04 D7
要发送特定的字节序列,请不要发送字符串——只需发送字节:
Dim process_CMD() As Byte = { &HA0, &H03, &H82, &H04, &HD7 }
ComPort.Write(process_CMD, 0, process_CMD.Length)
正如我在上面的评论中提到的,这些值只是数值。十六进制没有什么特别的。十六进制只是表示相同值的另一种方式。换句话说,上面的代码与此完全相同:
Dim process_CMD() As Byte = { 160, 3, 130, 4, 215 }
ComPort.Write(process_CMD, 0, process_CMD.Length)
如果字符串中包含十六进制数字,则可以使用 Convert.ToByte
方法的 appropriate overload 将十六进制数字的字符串表示形式转换为字节值。但是,一次只能转换一个字节,因此,首先您需要将字符串拆分为字节(每个字节两个十六进制数字。例如:
Dim input As String = "A0038204D7"
Dim bytes As New List(Of Byte)()
For i As Integer = 0 to input.Length Step 2
bytes.Add(Convert.ToByte(input.SubString(i, 2), 16)
Next
Dim process_CMD() As Byte = bytes.ToArray()
ComPort.Write(process_CMD, 0, process_CMD.Length)
但是,如果字符串的字节之间有空格,会更容易。那么你可以只使用 String.Split
方法:
Dim input As String = "A0 03 82 04 D7"
Dim hexBytes As String() = input.Split(" "c)
Dim bytes As New List(Of Byte)()
For Each hexByte As String in hexBytes
bytes.Add(Convert.ToByte(hexByte, 16)
Next
Dim process_CMD() As Byte = bytes.ToArray()
ComPort.Write(process_CMD, 0, process_CMD.Length)
或者,更简单地说:
Dim input As String = "A0 03 82 04 D7"
Dim process_CMD() As Byte = input.Split(" "c).Select(Function(x) Convert.ToByte(x, 16)).ToArray()
ComPort.Write(process_CMD, 0, process_CMD.Length)