将该字符串发送到它的十六进制等值 [英] Send the string to its hex equivalent

查看:173
本文介绍了将该字符串发送到它的十六进制等值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想发送十六进制命令到我的设备,因为它只能理解十六进制



因此,我设法创建一个函数来验证用户输入的是 string 是否有有效对应十六进制。问题是此处



因此,通过验证用户输入是否具有相应的十六进制相当于我相信我的系统发送的内容将被我的设备读取。 通过搜索我意识到它需要转换为字节,它状态


使用ASCIIEncoding类将字符串转换为您可以传输的字节数组
。 b


代码:




  Dim str as String =12345678
Dim bytes()as Byte = ASCIIEncoding.ASCII.GetBytes(strNumbers)
'然后发送te字节给读者
sp.Write(bytes,0,bytes.Length)




您不需要将值转换为HEX,在这种情况下,HEX是
,可以用不同的方式显示相同的东西。


我的代码:

 '这是一个字符串,值
Dim msg_cmd as string =A0038204D7
'将其转换为字节s o我的设备可以读取它
Dim process_CMD()As Byte = ASCIIEncoding.ASCII.GetBytes(msg_cmd)
'以字节形式发送
ComPort.Write(process_CMD,0,process_CMD.Length)

我的输出:

  41 30 30 33 38 32 30 34 44 37 

所需输出: p>

  A0 03 82 04 D7 


<要发送特定的字节序列,请不要发送字符串 - 只发送字节:

pre $ 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 方法。但是,它只能一次转换一个字节,因此,首先需要将字符串拆分为字节(每个字节两个十六进制数字)。例如:

  Dim input As String =A0038204D7
Dim bytes As New List(Byte)()
For I As Integer = 0 to input.Length步骤2
字节.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)


I would like to send hex commands to my device because it can only understand hex.

Because of that I manage to create a function that can validate if the users input which is a string has a valid corresponding hex. The question is here.

So, by validating if the users input has a corresponding hex equivalent I am confident that what my system sends will be read by my device. By searching I realized that it needs to be converted to bytes, it states

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.

My code:

'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) 

My Output:

41 30 30 33 38 32 30 34 44 37

Desired output:

A0 03 82 04 D7

解决方案

To send a specific sequence of bytes, don't send a string--just send the bytes:

Dim process_CMD() As Byte = { &HA0, &H03, &H82, &H04, &HD7 }
ComPort.Write(process_CMD, 0, process_CMD.Length)

As I mentioned in the comments above, the values are just numeric values. There is nothing special about hex. Hex is just another way to represent the same values. In other words, the code above does exactly the same thing as this:

Dim process_CMD() As Byte = { 160, 3, 130, 4, 215 }
ComPort.Write(process_CMD, 0, process_CMD.Length)

If you have the hex digits in a string, you can convert a string representation of a hex number to a byte value by using the appropriate overload of the Convert.ToByte method. However, that only converts one byte at a time, so, first you need to split the string into bytes (two hex digits per byte. For instance:

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)

However, it would be easier if the string had spaces between the bytes. Then you could just use the String.Split method:

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)

Or, more simply:

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)

这篇关于将该字符串发送到它的十六进制等值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆