Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Big-Endian to Little-Endian

9 views
Skip to first unread message

Terry Van Ausdall

unread,
Sep 19, 2003, 5:57:45 PM9/19/03
to
Does anyone know how to reverse the byte order of variables
for integer, long, single and double?

it is easy to do in C but I can not find a way to do it in VB6.

Any clues?

Terry

Grinder

unread,
Sep 19, 2003, 6:46:49 PM9/19/03
to

"Terry Van Ausdall" <tvana...@xicor.com> wrote in message
news:8d6601c37ef9$0e7c00b0$a601...@phx.gbl...

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

Grinder

unread,
Sep 19, 2003, 8:34:06 PM9/19/03
to
Here is a bit simpler version of FlipLong:

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

Larry Serflaten

unread,
Sep 19, 2003, 9:47:55 PM9/19/03
to
"Grinder" <gri...@no.spam.maam.com> wrote

> Here is a bit simpler version of FlipLong:

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

0 new messages