Please look at the long integer storage function in the array has errors

'Function: a long integer values stored in an array
Public Function ShutoShuzu(shu As Long, ByRef shuzu() As Byte, beg As Byte, zijie As Byte, Optional mode As Byte = 0) As Boolean 'Place an LONG value to a 4 byte array
On Error GoTo Errc
' Parameter 1, Shu as long to transfer, an array of 2, Shuzu Shu transformed storage, the starting position of 3, beg stored in the array.
' 4, Zijie will be stored in an array of 5 bytes, mode storage sequence

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) ' The number of head 2
sP2 = Mid(c, 3, 2) ' The number of times the high 2 bits
sP3 = Mid(c, 5, 2) ' The number of times the low 2 bits
sP4 = Mid(c, 7, 2) ' The number of minimum 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 sequence and long integer storage sequence with the same number in the content in the high byte in memory address low byte high low in memory access
' Byte 1: Buffer(0)-sP4
' Byte 2: Buffer(0)-sP4 Buffer(1)-sP3
' Byte 3: Buffer(0)-sP4 Buffer(1)-sP3 Buffer(2)-sP2
' Byte 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 and long integer in the content stored in reverse order number high byte in memory address low byte low in high memory access
' Byte 1: Buffer(0)-sP4
' Byte 2: Buffer(0)-sP3 Buffer(1)-sP4
' Byte 3: Buffer(0)-sP2 Buffer(1)-sP3 Buffer(2)-sP4
' Byte 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

Started by Lyle at February 07, 2016 - 6:01 AM

Code basically yes, just write better.

I first according to your idea, give you a little "optimization".:
'Function: a long integer values stored in an array
Public Function ShutoShuzu(shu As Long, ByRef shuzu() As Byte, beg As Byte, zijie As Byte, Optional mode As Byte = 0) As Boolean
'Place an LONG value to a 4 byte array
On Error GoTo Errc
' Parameter 1, Shu as long to be archived
' 2, Array Shuzu Shu transformed storage,
' 3, The initial position of the beg stored in the array.
' 4, Zijie will be stored in an array of bytes
' 5, Mode storage sequence
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) ' Only this sentence, to get you in front of a long string.

sP1 = Mid$(c, 1, 2) ' The number of head 2
sP2 = Mid$(c, 3, 2) ' The number of times the high 2 bits
sP3 = Mid$(c, 5, 2) ' The number of times the low 2 bits
sP4 = Mid$(c, 7, 2) ' The number of minimum 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 'Try to reduce the number of logical operations

'If mode <> 0 And mode <> 1 Then Exit Function
If (mode And 254) Then Exit Function ' That leaves 0 and 1 in two cases.!!!

' Mode =0 sequence and long integer storage sequence with the same number in the content in the high byte in memory address low byte high low in memory access
' Byte 1: Buffer(0)-sP4
' Byte 2: Buffer(0)-sP4 Buffer(1)-sP3
' Byte 3: Buffer(0)-sP4 Buffer(1)-sP3 Buffer(2)-sP2
' Byte 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 and long integer in the content stored in reverse order number high byte in memory address low byte low in high memory access
' ' Byte 1: Buffer(0)-sP4
' ' Byte 2: Buffer(0)-sP3 Buffer(1)-sP4
' ' Byte 3: Buffer(0)-sP2 Buffer(1)-sP3 Buffer(2)-sP4
' ' Byte 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 'Here you don't need, function has been assigned the entrance
MsgBox Err.Description
End Function

Posted by Norton at January 04, 2017 - 3:31 AM

Just noticed, your a = Hex (Shu) that also no need, no comments.

In your code, with the conversion of many of the string and numerical.
It affects the operation efficiency.

I give you write a new one, you can refer to:
'Numerical parameters of a single, best to use Long, or 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

Posted by Norton at January 04, 2017 - 5:47 AM

If you call, to ensure that the legitimate, and the value of parameter beg incoming and doesn't cause an array subscript overflow,
Then, the code, On Error GoTo and several statements related to can not.

Posted by Norton at January 10, 2017 - 4:26 AM

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

Posted by Dolores at January 14, 2017 - 5:19 AM