it is easy to do in C but I can not find a way to do it in VB6.
Any clues?
Terry
CopyMemory would probably be the most direct:
Private Declare Sub CopyMemory _
Lib "kernel32" _
Alias "RtlMoveMemory" ( _
Destination As Any, _
Source As Any, _
ByVal Length As Long)
Public Function FlipDouble(ByVal Number As Double) As Double
Dim dResult As Double
Dim pNumber As Long
Dim pResult As Long
Dim i As Long
pNumber = VarPtr(Number)
pResult = VarPtr(dResult) + Len(Number) - 1
For i = 0 To Len(Number) - 1
CopyMemory ByVal pResult, ByVal pNumber, 1
pNumber = pNumber + 1
pResult = pResult - 1
Next i
FlipDouble = dResult
End Function
Public Function FlipLong(ByVal Number As Long) As Long
Dim iResult As Long
Dim pNumber As Long
Dim pResult As Long
Dim i As Long
pNumber = VarPtr(Number)
pResult = VarPtr(iResult) + Len(Number) - 1
For i = 0 To Len(Number) - 1
CopyMemory ByVal pResult, ByVal pNumber, 1
pNumber = pNumber + 1
pResult = pResult - 1
Next i
FlipLong = iResult
End Function
Public Function FlipLong(ByVal Number As Long) As Long
Dim pNumber As Long
Dim pResult As Long
Dim i As Long
pNumber = VarPtr(Number)
pResult = VarPtr(FlipLong) + Len(Number) - 1
For i = 0 To Len(Number) - 1
CopyMemory ByVal pResult, ByVal pNumber, 1
pNumber = pNumber + 1
pResult = pResult - 1
Next i
End Function
Just as a reminder, if you are trying to shave cycles,
those 5 lines used to mangage the loop would have been
better spent unraveling the 4 byte loop.
(It will always be 4 bytes).
Public Function FlipLong(ByVal Number As Long) As Long
Dim Src As Long
Dim Dst As Long
Src = VarPtr(Number)
Dst = VarPtr(FlipLong)
CopyMemory ByVal Dst + 3, ByVal Src, 1
CopyMemory ByVal Dst + 2, ByVal Src + 1, 1
CopyMemory ByVal Dst + 1, ByVal Src + 2, 1
CopyMemory ByVal Dst, ByVal Src + 3, 1
End Function
Also, because 0 is a very common number, it might pay to
test for it and branch out, before doing anything in the routine.
Little else to do, so I thought I'd put in another 2 cents....
LFS