EndianReverse


Description:
This function uses the CopyMemory API two switch to and from little-endian and big-endian formats. This could be useful if you need to exchange data with other platforms.
 
Code:
Option Explicit

Private Declare Function CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
   (dest As Any, src As Any, ByVal length As Long) As Long

Private Sub EndianReverse(lpPointer As Long, nLen As Long)
    'Reverses the data pointed to by lpPointer
    ' nLen is the length of the data

    Dim nArray() As Byte
    ReDim nArray(1 To nLen)
    Dim i As Long
    
    'Copy the data to the byte array
    CopyMemory nArray(1), ByVal lpPointer, nLen
    
    'Copy the data from the byte array back to the main
    ' buffer, reversing the data as we go
    For i = 1 To nLen
        CopyMemory ByVal lpPointer + i - 1, nArray(nLen - i + 1), 1
    Next

End Sub
 
Sample Usage:
 
    Dim TheNumber As Long
    
    TheNumber = &H12345678
    EndianReverse VarPtr(TheNumber), Len(TheNumber)
    Debug.Print Hex(TheNumber) ' = 78563412