2个回答
展开全部
Private Function Conver_Hex(ByVal L As Long) As Array
Dim str_L As String = Hex(L)
If str_L.Length \ 2 <> 0 Then
str_L = "0" & str_L
End If
Dim byt(str_L.Length / 2 - 1) As Byte
For i = 0 To str_L.Length / 2 - 1
byt(i) = Convert.ToByte(str_L.Substring(2 * i, 2), 16)
Next
Return byt
End Function
'下面是调用代码
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim L As Double = 123456
Dim byt() As Byte = Conver_Hex(L)
End Sub
Dim str_L As String = Hex(L)
If str_L.Length \ 2 <> 0 Then
str_L = "0" & str_L
End If
Dim byt(str_L.Length / 2 - 1) As Byte
For i = 0 To str_L.Length / 2 - 1
byt(i) = Convert.ToByte(str_L.Substring(2 * i, 2), 16)
Next
Return byt
End Function
'下面是调用代码
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim L As Double = 123456
Dim byt() As Byte = Conver_Hex(L)
End Sub
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim a As Integer = 123456
Dim b As Byte() = Int2Arr(a)
For Each n In b
Me.Text &= " " & n
Next
End Sub
Private Function Int2Arr(num As Integer) As Byte()
Dim arr As New List(Of Byte)
While num > 0
arr.Insert(0, num Mod 256)
num = num \ 256
End While
Return arr.ToArray
End Function
End Class
结果为1 226 64,也就是你说的{&H01, &HE2 , &H40}
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim a As Integer = 123456
Dim b As Byte() = Int2Arr(a)
For Each n In b
Me.Text &= " " & n
Next
End Sub
Private Function Int2Arr(num As Integer) As Byte()
Dim arr As New List(Of Byte)
While num > 0
arr.Insert(0, num Mod 256)
num = num \ 256
End While
Return arr.ToArray
End Function
End Class
结果为1 226 64,也就是你说的{&H01, &HE2 , &H40}
追问
非常感谢您的回答!您的方法也不错,但是我已经找到更简单的方法了
Dim xx() As Byte = BitConverter.GetBytes(123456)
Array.Reverse(xx) '反序
PS:To知道:我写的内容没有意义吗?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询