VBspeed / Bits / SwapEndian
VBspeed © 2000-10, updated: 14-Sep-2004
SwapEndian


Function SwapEndian
Returns a Long with reversed byte order.

Example:
  SwapEndian(&H1245678) -->  2018915346 [= &H78563412]

Use this function (VB5/6-compatible) to verify the correctness of your code.
Code
SwapEndian01
Public Function SwapEndian01(ByVal dw As Long) As Long
' by Anonymous, not dated
  CopyMemory ByVal VarPtr(SwapEndian01) + 3, dw, 1
  CopyMemory ByVal VarPtr(SwapEndian01) + 2, ByVal VarPtr(dw) + 1, 1
  CopyMemory ByVal VarPtr(SwapEndian01) + 1, ByVal VarPtr(dw) + 2, 1
  CopyMemory SwapEndian01, ByVal VarPtr(dw) + 3, 1
End Function
SwapEndian02
Private Type T1Long
  lDWord As Long
End Type
Private Type T4Byte
  bByte1 As Byte  'lo
  bByte2 As Byte
  bByte3 As Byte
  bByte4 As Byte  'hi
End Type

Public Function SwapEndian02(ByVal dw As Long) As Long
' by Matthew Curland, mattcur@microsoft.com, 20010601
  Dim u4Byte1 As T4Byte
  Dim u4Byte2 As T4Byte
  Dim u1Long As T1Long
  u1Long.lDWord = dw
  LSet u4Byte1 = u1Long
  With u4Byte1
    u4Byte2.bByte1 = .bByte4
    u4Byte2.bByte2 = .bByte3
    u4Byte2.bByte3 = .bByte2
    u4Byte2.bByte4 = .bByte1
  End With
  LSet u1Long = u4Byte2
  SwapEndian02 = u1Long.lDWord
End Function
SwapEndian03
Public Function SwapEndian03(ByVal dw As Long) As Long
' by Donald, donald@xbeat.net, 20040808
  Dim b1 As Long
  Dim b2 As Long
  Dim b3 As Long
  Dim b4 As Long
  Dim lHighBit As Long
  
  ' handle sign bit
  lHighBit = dw And &H80000000
  If lHighBit Then
    dw = dw And Not &H80000000
  End If
  
  b1 = dw And &HFF
  b2 = (dw And &HFF00&) \ &H100&
  b3 = (dw And &HFF0000) \ &H10000
  b4 = (dw And &HFF000000) \ &H1000000
  
  If lHighBit Then
    b4 = b4 Or &H80
  End If
  
  If b1 And &H80& Then
    SwapEndian03 = ((b1 And &H7F&) * &H1000000 Or &H80000000) + _
        b2 * &H10000 + b3 * &H100& + b4
  Else
    SwapEndian03 = b1 * &H1000000 + b2 * &H10000 + b3 * &H100& + b4
  End If
  
End Function
SwapEndian04
Public Function SwapEndian04(ByVal dw As Long) As Long
' by FireBot, fire_bot@hotmail.com, 20040809
  Dim lngTmp As Long
  Dim dblTmp As Double
  dblTmp = CDbl(dw And &HFF&) * &H1000000
  If dblTmp > 2147483647 Then lngTmp = dblTmp - 4294967296# Else lngTmp = dblTmp
  lngTmp = lngTmp Or ((dw And &HFF00&) * &H100)
  lngTmp = lngTmp Or ((dw And &HFF0000) \ &H100)
  lngTmp = lngTmp Or (((dw And &HFF000000) \ &H1000000) And &HFF)
  SwapEndian04 = lngTmp
End Function
SwapEndian05
Public Function SwapEndian05(ByVal dw As Long) As Long
' by Paul, wpsjr1@syix.com, 20040809
  Dim b1 As Long
  Dim b2 As Long
  Dim b3 As Long
  Dim b4 As Long
  Dim lHighBit As Long
  Dim lLoBit As Long

  lHighBit = dw And &H80000000
  dw = dw And &H7FFFFFFF

  b1 = dw And &H7F
  lLoBit = dw And &H80
  b2 = (dw And &HFF00&)
  b3 = (dw And &HFF0000) \ &H100
  b4 = dw \ &H1000000

  b4 = b4 Or ((lHighBit \ &H1000000) And &HFF)

  SwapEndian05 = (b1 * &H1000000) Or (b2 * &H100) Or b3 Or b4
  If lLoBit Then SwapEndian05 = SwapEndian05 Or &H80000000
End Function
SwapEndian06
Private Declare Function htonl Lib "wsock32.dll" (ByVal hostlong As Long) As Long

Public Function SwapEndian06(ByVal dw As Long) As Long
' by Paul, wpsjr1@syix.com, 20040809
  SwapEndian06 = htonl(dw)
End Function
SwapEndian07
Public Function SwapEndian07(ByVal dw As Long) As Long
' by Donald, donald@xbeat.net, 20040809
  Dim b1 As Long
  Dim b2 As Long
  Dim b3 As Long
  Dim b4 As Long
  Dim lHighBit As Long
  
  lHighBit = dw And &H80000000
  If lHighBit Then
    dw = dw And Not &H80000000
  End If
  
  b1 = dw And &HFF
  b2 = (dw And &HFF00&) \ &H100&
  b3 = (dw And &HFF0000) \ &H10000
  If lHighBit Then
    b4 = dw \ &H1000000 Or &H80
  Else
    b4 = dw \ &H1000000
  End If
  
  If b1 And &H80& Then
    SwapEndian07 = ((b1 And &H7F&) * &H1000000 Or &H80000000) Or _
        b2 * &H10000 Or b3 * &H100& Or b4
  Else
    SwapEndian07 = b1 * &H1000000 Or _
        b2 * &H10000 Or b3 * &H100& Or b4
  End If
  
End Function
SwapEndian08
Public Function SwapEndian08(ByVal dw As Long) As Long
' by Mike D Sutton, Mike.Sutton@btclick.com, 20040914
  SwapEndian08 = _
      (((dw And &HFF000000) \ &H1000000) And &HFF&) Or _
      ((dw And &HFF0000) \ &H100&) Or _
      ((dw And &HFF00&) * &H100&) Or _
      ((dw And &H7F&) * &H1000000)
  If (dw And &H80&) Then SwapEndian08 = SwapEndian08 Or &H80000000
End Function
Calls
1sRet = SwapEndian(&H12345678) --> [=&H78563412]
2sRet = SwapEndian(&H80&) --> [=&H80000000]
3sRet = SwapEndian(&H80000000) --> [=&H80&]
4sRet = SwapEndian(&H80808080) --> [=&H80808080]
Charts
 VB5 Charts
CodeAuthorDopingNotes
SwapEndian01 AnonymousAPI 
SwapEndian02 Matthew  
SwapEndian03 Donald  
SwapEndian04 FireBot  
SwapEndian05 Paul  
SwapEndian06 PaulAPI 
SwapEndian06 PaulAPI[inline]
SwapEndian07 Donald  
SwapEndian08 Mike  
Call 1
917.270.189Ás
85.940.065Ás
31.500.016Ás
72.600.028Ás
21.470.016Ás
62.020.022Ás
51.730.019Ás
41.580.017Ás
11.000.011Ás
Call 2
918.080.190Ás
86.190.065Ás
31.620.017Ás
72.860.030Ás
51.840.019Ás
62.100.022Ás
41.790.019Ás
21.590.017Ás
11.000.011Ás
Call 3
917.230.189Ás
85.920.065Ás
41.730.019Ás
72.610.029Ás
21.440.016Ás
62.020.022Ás
31.720.019Ás
51.930.021Ás
11.000.011Ás
Call 4
918.060.189Ás
86.210.065Ás
62.120.022Ás
72.850.030Ás
31.870.020Ás
52.090.022Ás
21.780.019Ás
41.930.020Ás
11.000.010Ás
 VB6 Charts
CodeAuthorDopingNotes
SwapEndian01 AnonymousAPI 
SwapEndian02 Matthew  
SwapEndian03 Donald  
SwapEndian04 FireBot  
SwapEndian05 Paul  
SwapEndian06 PaulAPI 
SwapEndian06 PaulAPI[inline]
SwapEndian07 Donald  
SwapEndian08 Mike  
Call 1
920.620.207Ás
85.880.059Ás
21.560.016Ás
72.900.029Ás
31.560.016Ás
62.540.025Ás
52.220.022Ás
41.620.016Ás
11.000.010Ás
Call 2
920.220.207Ás
85.760.059Ás
31.650.017Ás
73.000.031Ás
41.800.018Ás
62.470.025Ás
52.190.022Ás
21.530.016Ás
11.000.010Ás
Call 3
920.770.207Ás
85.890.059Ás
31.840.018Ás
72.890.029Ás
21.600.016Ás
62.540.025Ás
52.230.022Ás
41.930.019Ás
11.000.010Ás
Call 4
920.300.207Ás
85.770.059Ás
42.030.021Ás
73.000.031Ás
21.810.018Ás
62.470.025Ás
52.190.022Ás
31.850.019Ás
11.000.010Ás
Conclusions
SwapEndian01 is the most popular solution in the web, however, as you see, it's 20 times slower than necessary.
Got comments? How to read all those numbers

top




VBspeed © 2000-10 by Donald Lessau