送分100分:如何将一个LONG型变量的值分配到一个数组的4个BYTE成员中
问题如下:1、如何将一个LONG型变量的值分配到一个数组的4个BYTE成员中,分配可能不是按顺序,也就是说LONG型变量的1-4字节,与数组的1-4字节的顺序可能不是一一对应的。
2、而后如何将这4个BYTE的内容迅速填充到这个LONG型变量的相应字节中。
--------------------编程问答--------------------
Option Explicit
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Dim Buffer(3) As Byte
Private Sub LngToBin_Click()
Dim i As Integer
Dim hexData As String
Dim a As Long
a = Val(Text1)
CopyMemory Buffer(0), a, 4
For i = 0 To UBound(Buffer)
Debug.Print Buffer(i)
Next
End Sub
Private Sub BinToLng_Click()
Dim sinStr As String
Dim sinSj As Long
Dim i As Integer
CopyMemory ByVal VarPtr(sinSj), ByVal VarPtr(Buffer(0)), 4
Text2 = sinSj
End Sub
Private Sub Form_Load()
Text1 = ""
Text2 = ""
End Sub
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/zdingyun/archive/2009/09/18/4565659.aspx
至于字节顺序的调整LZ自行根据实际情况调整. --------------------编程问答-------------------- 顺序不定?有什么映射关系 要不你转化过去,也回不来 --------------------编程问答-------------------- 我现在关心的就是除了COPYMEMORY之外,有没有其他办法,这个问题其实也应该包括DOUBLE变量。 --------------------编程问答--------------------
如果不用copymemory那就用与运算:
Dim a As Long--------------------编程问答-------------------- 按4楼说的那种方法比较好,不过处理负数时还要改动一下 --------------------编程问答-------------------- 为什么不用copymemory?这个是速度最快的转换方式了 --------------------编程问答-------------------- 因为.net中不能用COPYMEMORY函数。 --------------------编程问答--------------------
a = &H23FFCCEE
Dim b(3) As Byte
b(0) = (a And &HFF000000) / &H1000000
b(1) = (a And &HFF0000) / &H10000
b(2) = (a And &HFF00&) / &H100&
b(3) = a And &HFF
Dim i As Integer
For i = 0 To 3
Debug.Print Hex(b(i))
Next i
你说的很对。 --------------------编程问答--------------------
Option Explicit
Dim Buffer(3) As Byte
Dim i As Long
Private Sub Command1_Click()
Dim lnSj As Long
Dim hexSj As String
lnSj = Val(Text1)
hexSj = Right("0000000" & Hex(lnSj), 8)
For i = 1 To Len(hexSj) Step 2
Buffer((i - 1) / 2) = "&H" & Mid(hexSj, i, 2)
Next
Print hexSj
For i = 0 To 3
Print Buffer(i)
Next
End Sub
Private Sub Command2_Click()
Dim hexSj As String
Dim lnSj As Long
For i = 0 To 3
hexSj = hexSj & Right("0" & Hex(Buffer(i)), 2)
Next
Print Val("&H" & hexSj & "&")
End Sub
关于DOUBLE变量及SINGLE变量参阅:VB的浮点数处理 --------------------编程问答--------------------
--------------------编程问答-------------------- 自定义类型 + Lset 在VB6中很好用,负数也没问题
'自定义类型
Private Type myLong
val As Long
End Type
Private Type myDouble
val As Double
End Type
Private Type Bt4
val(3) As Byte
End Type
Private Type Bt8
val(7) As Byte
End Type
Sub tttt()
Dim tpLong As myLong, tpdou As myDouble
Dim tpBt4 As Bt4, tpBt8 As Bt8
'数值到字节
tpLong.val = 123456
LSet tpBt4 = tpLong
For i = 0 To 3
Debug.Print tpBt4.val(i)
Next
tpdou.val = 9874563215.6665
LSet tpBt8 = tpdou
For i = 0 To 7
Debug.Print tpBt8.val(i)
Next
''''''''''数值归零''''''''''''''
tpdou.val = 0
tpLong.val = 0
'字节到数值
LSet tpLong = tpBt4
LSet tpdou = tpBt8
Debug.Print tpLong.val, tpdou.val
End Sub
.net 中没有试过 --------------------编程问答-------------------- 10#高手 --------------------编程问答--------------------
--------------------编程问答--------------------
Private Sub Command1_Click()
Dim a As Long, b As String, c(3) As Byte
Dim d As Long, i As Byte
a = -123456789
b = Format(Hex(a), "00000000")
For i = 0 To 3
c(i) = "&H" & Mid(b, i * 2 + 1, 2)
Next
b = ""
For i = 0 To 3
b = b & Hex(c(i))
Next
d = "&H" & b
End Sub
如果想转换double,请按ieee浮点数格式转换 --------------------编程问答--------------------
copymem不是最快的方式
补充:VB , 网络编程