请大家帮忙看一下这个长整型数 存放 到 数组中的函数有错误吗
'函数功能:转一个长整型数值 存放在一个数组中--------------------编程问答-------------------- 代码基本上没错,只是写得比较差劲。
Public Function ShutoShuzu(shu As Long, ByRef shuzu() As Byte, beg As Byte, zijie As Byte, Optional mode As Byte = 0) As Boolean '将一个LONG型数值放置到一个4字节数组中
On Error GoTo Errc
' 参数 1、shu 为要转存的长整数, 2、shuzu 为shu转化后存放的数组, 3、beg 存放在数组中的起始位置
' 4、zijie 要在数组中存放几个字节 5、mode 存放顺序
Dim a As String, sP4 As String, sP3 As String, sP2 As String, sP1 As String
Dim B As Byte, c As String
ShutoShuzu = False
a = hex(shu)
B = Len(a)
Select Case B
Case 1
c = "0000000" & a
Case 2
c = "000000" & a
Case 3
c = "00000" & a
Case 4
c = "0000" & a
Case 5
c = "000" & a
Case 6
c = "00" & a
Case 7
c = "0" & a
Case 8
c = a
Case Else
Exit Function
End Select
sP1 = Mid(c, 1, 2) ' 数的头高2位
sP2 = Mid(c, 3, 2) ' 数的次高2位
sP3 = Mid(c, 5, 2) ' 数的次低2位
sP4 = Mid(c, 7, 2) ' 数的最低2位
If zijie <> 1 And zijie <> 2 And zijie <> 3 And zijie <> 4 Then Exit Function
If mode <> 0 And mode <> 1 Then Exit Function
' mode =0 顺序 与 长整型数在内容中存放顺序相同 数的高字节在 内存高址 低字节在内存低址
' 字节为1: Buffer(0)-sP4
' 字节为2: Buffer(0)-sP4 Buffer(1)-sP3
' 字节为3: Buffer(0)-sP4 Buffer(1)-sP3 Buffer(2)-sP2
' 字节为4: Buffer(0)-sP4 Buffer(1)-sP3 Buffer(2)-sP2 Buffer(3)-sP1
If mode = 0 Then
Select Case zijie
Case 1
shuzu(beg) = Val("&h" & sP4)
Case 2
shuzu(beg) = Val("&h" & sP4)
shuzu(beg + 1) = Val("&h" & sP3)
Case 3
shuzu(beg) = Val("&h" & sP4)
shuzu(beg + 1) = Val("&h" & sP3)
shuzu(beg + 2) = Val("&h" & sP2)
Case 4
shuzu(beg) = Val("&h" & sP4)
shuzu(beg + 1) = Val("&h" & sP3)
shuzu(beg + 2) = Val("&h" & sP2)
shuzu(beg + 3) = Val("&h" & sP1)
End Select
End If
' mode =1 与 长整型数在内容中存放顺序相反 数的高字节在 内存低址 低字节在内存高址
' 字节为1: Buffer(0)-sP4
' 字节为2: Buffer(0)-sP3 Buffer(1)-sP4
' 字节为3: Buffer(0)-sP2 Buffer(1)-sP3 Buffer(2)-sP4
' 字节为4: Buffer(0)-sP1 Buffer(1)-sP2 Buffer(2)-sP3 Buffer(3)-sP4
If mode = 1 Then
Select Case zijie
Case 1
shuzu(beg) = Val("&h" & sP4)
Case 2
shuzu(beg) = Val("&h" & sP3)
shuzu(beg + 1) = Val("&h" & sP4)
Case 3
shuzu(beg) = Val("&h" & sP2)
shuzu(beg + 1) = Val("&h" & sP3)
shuzu(beg + 2) = Val("&h" & sP4)
Case 4
shuzu(beg) = Val("&h" & sP1)
shuzu(beg + 1) = Val("&h" & sP2)
shuzu(beg + 2) = Val("&h" & sP3)
shuzu(beg + 3) = Val("&h" & sP4)
End Select
End If
ShutoShuzu = True
Exit Function
Errc:
ShutoShuzu = False
MsgBox ERR.Description
End Function
我先按你的想法,给你稍微“优化”一下:
'函数功能:转一个长整型数值 存放在一个数组中--------------------编程问答-------------------- 刚注意到,你的 a = Hex(shu) 那句也用不着了,没注释掉。
Public Function ShutoShuzu(shu As Long, ByRef shuzu() As Byte, beg As Byte, zijie As Byte, Optional mode As Byte = 0) As Boolean
'将一个LONG型数值放置到一个4字节数组中
On Error GoTo Errc
' 参数 1、shu 为要转存的长整数
' 2、shuzu 为shu转化后存放的数组,
' 3、beg 存放在数组中的起始位置
' 4、zijie 要在数组中存放几个字节
' 5、mode 存放顺序
Dim a As String, sP4 As String, sP3 As String, sP2 As String, sP1 As String
Dim B As Byte, c As String
ShutoShuzu = False
a = Hex(shu)
'B = Len(a)
'Select Case B
' Case 1
' c = "0000000" & a
' Case 2
' c = "000000" & a
' Case 3
' c = "00000" & a
' Case 4
' c = "0000" & a
' Case 5
' c = "000" & a
' Case 6
' c = "00" & a
' Case 7
' c = "0" & a
' Case 8
' c = a
' Case Else
' Exit Function
'End Select
c = Right$("0000000" & Hex$(shu), 8) ' 只这句,就搞定你前面的一长串了
sP1 = Mid$(c, 1, 2) ' 数的头高2位
sP2 = Mid$(c, 3, 2) ' 数的次高2位
sP3 = Mid$(c, 5, 2) ' 数的次低2位
sP4 = Mid$(c, 7, 2) ' 数的最低2位
'If zijie <> 1 And zijie <> 2 And zijie <> 3 And zijie <> 4 Then Exit Function
If (zijie < 1 Or zijie > 4) Then Exit Function '尽量减少逻辑运算的数量
'If mode <> 0 And mode <> 1 Then Exit Function
If (mode And 254) Then Exit Function ' 这样就只剩下 0和1 两种情况了!!!
' mode =0 顺序 与 长整型数在内容中存放顺序相同 数的高字节在 内存高址 低字节在内存低址
' 字节为1: Buffer(0)-sP4
' 字节为2: Buffer(0)-sP4 Buffer(1)-sP3
' 字节为3: Buffer(0)-sP4 Buffer(1)-sP3 Buffer(2)-sP2
' 字节为4: Buffer(0)-sP4 Buffer(1)-sP3 Buffer(2)-sP2 Buffer(3)-sP1
If mode = 0 Then
Select Case zijie
Case 1
shuzu(beg) = Val("&h" & sP4)
Case 2
shuzu(beg) = Val("&h" & sP4)
shuzu(beg + 1) = Val("&h" & sP3)
Case 3
shuzu(beg) = Val("&h" & sP4)
shuzu(beg + 1) = Val("&h" & sP3)
shuzu(beg + 2) = Val("&h" & sP2)
Case 4
shuzu(beg) = Val("&h" & sP4)
shuzu(beg + 1) = Val("&h" & sP3)
shuzu(beg + 2) = Val("&h" & sP2)
shuzu(beg + 3) = Val("&h" & sP1)
End Select
' End If
' ' mode =1 与 长整型数在内容中存放顺序相反 数的高字节在 内存低址 低字节在内存高址
' ' 字节为1: Buffer(0)-sP4
' ' 字节为2: Buffer(0)-sP3 Buffer(1)-sP4
' ' 字节为3: Buffer(0)-sP2 Buffer(1)-sP3 Buffer(2)-sP4
' ' 字节为4: Buffer(0)-sP1 Buffer(1)-sP2 Buffer(2)-sP3 Buffer(3)-sP4
' If mode = 1 Then
Else
Select Case zijie
Case 1
shuzu(beg) = Val("&h" & sP4)
Case 2
shuzu(beg) = Val("&h" & sP3)
shuzu(beg + 1) = Val("&h" & sP4)
Case 3
shuzu(beg) = Val("&h" & sP2)
shuzu(beg + 1) = Val("&h" & sP3)
shuzu(beg + 2) = Val("&h" & sP4)
Case 4
shuzu(beg) = Val("&h" & sP1)
shuzu(beg + 1) = Val("&h" & sP2)
shuzu(beg + 2) = Val("&h" & sP3)
shuzu(beg + 3) = Val("&h" & sP4)
End Select
End If
ShutoShuzu = True
Exit Function
Errc:
'ShutoShuzu = False '这儿用不着了,函数入口已经赋值了
MsgBox Err.Description
End Function
你的代码中,用了不少的字符串与数值的相互转换。
这样比较影响运行效率。
我给你重新写一个,你可以参考一下:
'单个的数值参数,最好用Long,或者用 Integer
Public Function ShutoShuzu(shu As Long, ByRef shuzu() As Byte, beg As Long, zijie As Long, Optional mode As Long = 0) As Boolean
Dim lMASK As Long, lDIV As Long
Dim i&, j&, p&
On Error GoTo Errc
ShutoShuzu = False
If (zijie < 1 Or zijie > 4) Then Exit Function
If (mode And -2&) Then Exit Function
p = beg
If (mode = 0&) Then
lMASK = -1
lDIV = 1
For i = 1 To zijie
shuzu(p) = (shu And lMASK) \ lDIV And 255
If (i And 3) Then lMASK = lMASK * 256
lDIV = (lDIV And &HFFFFFF) * 256
p = p + 1
Next
Else
lMASK = &HFF000000
lDIV = &H1000000
For i = 1 To zijie
shuzu(p) = (shu And lMASK) \ lDIV And 255
lMASK = lMASK \ 256
lDIV = lDIV \ 256
p = p + 1
Next
End If
ShutoShuzu = True
Exit Function
Errc:
MsgBox Err.Description, vbExclamation
End Function
--------------------编程问答-------------------- 如果你调用时,能确保传入的参数 beg 的值合法、并且不会造成数组下标溢出,
那么,我那个代码中, On Error GoTo 及相关的几个语句都可以不要。
--------------------编程问答--------------------
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Public Function ShutoShuzu(shu As Long, ByRef shuzu() As Byte, beg As Byte, zijie As Byte, Optional mode As Byte = 0) As Boolean
Dim bytBuff(3) As Byte, bytTmp As Byte, i As Integer, j As Integer
If (zijie > 4) Or (beg + zijie > Ubond(shuzu)) Then
Exit Function
End If
CopyMemory bytBuff, shu, 4
If mode Then
bytTmp = bytBuff(1)
bytBuff(1) = bytBuff(4)
bytBuff(4) = bytTmp
bytTmp = bytBuff(2)
bytBuff(2) = bytBuff(3)
bytBuff(3) = bytTmp
End If
j = 0
For i = beg To beg + zijie - 1
shuzu(i) = bytBuff(j)
j = j + 1
Next i
ShutoShuzu = True
End Function
补充:VB , 基础类