请问有没有办法使Byte型数组转换成long型数组
我这里有一个3维byte型数组,是存储位图数据的,但是在分析数据的时候,我想把他们都转换成long型来计算,我想问问有没有办法在vb中直接把这个数组转换成long型,clng貌似不顶用,除了每个成员逐个转换外
[解决办法]
b(0),b(1),b(2)
L=b(0)+256*b(1)+b(2)*65536
[解决办法]
Option ExplicitPrivate Declare Sub GetMem4 Lib "msvbvm60" (ByVal Addr As Long, RetVal As Long)Private Sub Command1_Click() Dim b(2) As Byte Dim lRlt As Long ' b数组 中的字节顺序要与 Long 类型的内存字节序一致 b(0) = &H12 b(1) = &H34 b(2) = &H56 Call GetMem4(VarPtr(b(0)), lRlt) lRlt = lRlt And &HFFFFFF ' 把最高字节屏蔽掉 MsgBox Hex$(lRlt), 64 ' 输出:563412End Sub
[解决办法]
两种数组逐个赋值得了,速度也很快,印象中标准的汇编里是没这种批量转换的,实在还嫌速度不够就要去MMX,SSE这类东西里找。
不过我要提醒你一下,你这个测速肯定是要编译成EXE后测试,你别在VBIDE里测,那不是执行的编译后的机器码,速度会慢很多的
[解决办法]
转换数组要方便的话,觉得还是用CopyMemory吧
Private Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" ( _ ByRef Destination As Any, _ ByRef Source As Any, _ ByVal Length As Long)'ByteArray()为要转换的Byte数组'LongArray()为接收转换后数据的Long数组,下标从0开始,最好定义为可变数组,或传入的数组长度必须能容纳Byte数组,否则内存将会越界'成功返回True,失败(字节数组长度不是4的倍数或为空)返回FalsePrivate Function TransByteArrayToLongArray(ByteArray() As Byte, LongArray() As Long) As Boolean Dim ByteArrayLen As Long On Error Resume Next ByteArrayLen = UBound(ByteArray) - LBound(ByteArray) + 1 If Err.Number = 0 And ByteArrayLen > 0 And ByteArrayLen Mod 4 = 0 Then ReDim LongArray(ByteArrayLen / 4 - 1) CopyMemory ByVal VarPtr(LongArray(0)), ByVal VarPtr(ByteArray(LBound(ByteArray))), ByteArrayLen TransByteArrayToLongArray = True End IfEnd Function'测试Private Sub Command1_Click() Dim a(7999) As Byte, b() As Long Dim i As Integer For i = 0 To 7999 a(i) = i Mod &H100 Next i TransByteArrayToLongArray a, b For i = 0 To UBound(b) Debug.Print Hex(b(i)) Next iEnd Sub
[解决办法]
又看了楼主说的,好像搞错了,原来是将字节数组直接变为长整型数组,值又要一样,那就没有什么方便的办法吧,直接赋值就行了,好像也只能逐个直接赋值吧
Dim a(3) as byte,b(3) as longb(0)=a(0):b(1)=a(1):b(2)=a(2):b(3)=a(3)