Public Function dec_to_hex(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 "0": a = "30H"
Case "1": a = "31H"
Case "2": a = "32H"
Case "3": a = "33H"
Case "4": a = "34H"
Case "5": a = "35H"
Case "6": a = "36H"
Case "7": a = "37H"
Case "8": a = "38H"
Case "9": a = "30H"
Case "10": a = "65H"
Case "11": a = "66H"
Case "12": a = "67H"
Case "13": a = "68H"
Case "14": a = "69H"
Case "15": a = "70H"
End Select
dec_to_hex = a " " dec_to_hex
dec = dec \ 16
Loop
End Function
Dim s As String = "h" Hex(Asc(Val(InputBox("输入数值", "提示", 1))))
MsgBox(s)
VB.Net自带函数:
十进制转八进制:Oct(num)
十六进制转八进制:oct("H" num)
十进制转十六进制:hex(num)
八进制转十六进制:hex("O" num)
十六进制转换为十进制:CLng("H" num)
下面代码就是将文本框中以一个空格隔开的十六进制转为字节的代码,文本框中数字格式为: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)