Often times I find myself trying to write a file for another program to read, or read a file written by another program, in which that other program uses Big Endian byte order, even though VB6 uses Little Endian byte order. I've used CopyMemory solutions before, which are easy to write as you don't have to think about how to multiply and divide in order to perform bit shifts. I've even written a DLL file in assembly language (see this thread http://www.vbforums.com/showthread.p...89#post5000089 ) which has functions that directly use the SHL and SHR bit shifting opcodes, but then I need to make sure my DLL file is always packaged with my VB6 program or the program won't run on somebody else's computer when they try to run it. So below is my code that uses only mathematical operations to rearange the bytes in Integer and Long data types in order to swap byte order. Any Little Endian number will be converted to Big Endian, and any Big Endian number will be converted to Little Endian.
Change Private to Public if you are going to put these function definitions in a Module and plan to call them from elsewhere in your program.
The one type of thing that you CAN'T swap byte order with, using only mathematical techniques, is a floating point (Single or Double) variable. Currency type variables MIGHT be able to be byte swapped with purely mathematical techniques, but it would be quite difficult, due to the multiple of 10000 that you need to take into account. Short of using a dedicated DLL file, or a CopyMemory technique, floating point values will remain Little Endian when working with VB6.
Code:
Private Function FixInteger(ByVal Value As Integer) As Integer
FixInteger = (Value And &H7FFF) \ &H100
If Value < 0 Then FixInteger = FixInteger Or &H80&
FixInteger = FixInteger Or (Value And &H7F&) * &H100
If Value And &H80 Then FixInteger = FixInteger Or &H8000
End Function
Private Function FixLong(ByVal Value As Long) As Long
FixLong = (Value And &H7FFFFFFF) \ &H1000000
If Value < 0 Then FixLong = FixLong Or &H80&
FixLong = FixLong Or (Value And &HFF00&) * &H100&
FixLong = FixLong Or (Value And &HFF0000) \ &H100&
FixLong = FixLong Or (Value And &H7F&) * &H1000000
If Value And &H80& Then FixLong = FixLong Or &H80000000
End Function
The one type of thing that you CAN'T swap byte order with, using only mathematical techniques, is a floating point (Single or Double) variable. Currency type variables MIGHT be able to be byte swapped with purely mathematical techniques, but it would be quite difficult, due to the multiple of 10000 that you need to take into account. Short of using a dedicated DLL file, or a CopyMemory technique, floating point values will remain Little Endian when working with VB6.