Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
创新互联是一家企业级云计算解决方案提供商,超15年IDC数据中心运营经验。主营GPU显卡服务器,站群服务器,重庆服务器托管,海外高防服务器,机柜大带宽,动态拨号VPS,海外云手机,海外云服务器,海外服务器租用托管等。
MsgBox(DEC_to_HEX(255))
End Sub
Public Function DEC_to_HEX(ByVal Dec As Long) As String
Dim a As String
DEC_to_HEX = ""
Do While Dec 0
a = CStr(Dec Mod 16)
Select Case a
Case "10" : a = "A"
Case "11" : a = "B"
Case "12" : a = "C"
Case "13" : a = "D"
Case "14" : a = "E"
Case "15" : a = "F"
End Select
DEC_to_HEX = a DEC_to_HEX
Dec = Dec \ 16
Loop
End Function
下面代码就是将文本框中以一个空格隔开的十六进制转为字节的代码,文本框中数字格式为:01 02 03
Dim TestArray() As String = Split(TextBox1.Text)
Dim hexBytes() As Byte
ReDim hexBytes(TestArray.Length - 1)
Dim i As Integer
For i = 0 To TestArray.Length - 1
hexBytes(i) = Val("h" TestArray(i))
Next
SerialPort.Write(hexBytes, 0, hexBytes.Length)
应该是你的转换格式不正确,下面代码就是将文本框中以一个空格隔开的十六进制转为字节的代码,文本框中数字格式为:01 02 03
Dim TestArray() As String = Split(TextBox1.Text)
Dim hexBytes() As Byte
ReDim hexBytes(TestArray.Length - 1)
Dim i As Integer
For i = 0 To TestArray.Length - 1
hexBytes(i) = Val("h" TestArray(i))
Next
SerialPort.Write(hexBytes, 0, hexBytes.Length)
如果有问题可以再联系。
在内存中没有所谓得16进制或者其他进制,数就是数,什么进制是人自己理解的,而不是计算机。你所谓的16进制大概时你输入的字符序列,你自己需要把这个字符序列转换为数。
也许你要的是
Dim hex As Integer = H50 '这种定义
numHex =Integer.Parse(strHex, Globalization.NumberStyles.AllowHexSpecifier)
或
numHex = Integer. Parse("H" strHex)