SwapByteOrder
Result = SwapByteOrder(Value)
 
Parameters:

    Value = The value whose byte order you want to swap
Returns:

    Result
 

     SwapByteOrder splits an 32bit integer value into 4 bytes, reverses the order of these bytes and returns the result as an integer value.

For example the binary representation of the decimal value 16909320 is
  
  00000001 00000010 00000100 00001000
  


Now we split this value into 4 bytes named A, B, C and D
  
  (A)00000001 (B)00000010 (C)00000100 (D)00001000
  

and change the order to D, C, B and A
  
  (D)00001000 (C)00000100 (B)00000010 (A)00000001
  

So the binary equivalent of what SwapByteOrder(16909320) returns would be
  
  00001000 00000100 00000010 00000001
  

which is decimal 134480385.




FACTS:


      * SwapByteOrder is often used when reading / writing data. Something you might not be aware is that different computers / platforms have different ideas about how that same data should be stored. For example when we write Integers to memory on PC (Intel/AMD) platforms the bytes are stored lowest to highest byte. Where as other platforms store integers highest to lowest. Often called Motorola format. So If you read a file expecting the data within it to be stored in Intel/Amd format, then you can sometimes be surprised. If that occurs you'd use SwapByteOrder to flip the bytes.



Mini Tutorial #1:


      Showing the effect of SwapByteOrder

  
; Assign the variable Value the Hex value $AABBCCDD
  Value=$AABBCCDD
  
; Show the HEX$ representation of value
  Print Hex$(Value)
  
; show the BEX$y representation of value after using SwapByteOrder\
  Print Hex$(SwapByteOrder(Value))
  
; Display the Screen and wait for the user to press a key
  Sync
  WaitKey
  



This example would output.

  
  $AABBCCDD
  $DDCCBBAA
  





Mini Tutorial #2:


      This example writes an integer to memory then shows the byte order of how the data is stored in memory. It then flips the bytes within the integer and display the results.


  
  
; Creating a banks that's 4 bytes long
  ThisBank=NewBank(4)
  
; This the value we're storing in memory
  ThisValue     = $AABBCCDD
  
; poke 32bit (4bytes) in bank
  PokeBankInt ThisBank,0,ThisValue
  
; render this to the screen
  Print "Intel Order"
  Gosub ShowInfo
  
  
; poke 32bit (4bytes) in bank
  PokeBankInt ThisBank,0,SwapByteOrder(ThisValue)
  
; render this to the screen
  Print "Motorola Order"
  Gosub ShowInfo
  
  
; release the bank
  DeleteBank ThisBank
  
; Display the Screen and wait for the user to press a key
  Sync
  WaitKey
  
  
; end program
  End
  
  
  
ShowInfo:
  
; Peek the data back
  ThisInt=PeekBankInt(ThisBank,0)
  
; Show this value
  Print "Hex:"+Hex$(ThisINt)
  
; read the bytes and show the ORDER
  For lp =0 To 3
     ThisByte=PeekBankByte(ThisBank,lp)
     s$="$"+Right$(Hex$(ThisByte),2)
     Print s$
  Next
  
  Print ""
  
  Return
  
  


This example would output.


  
  Intel Order
  Hex:$AABBCCDD
  $DD
  $CC
  $BB
  $AA
  
  Motorola Order
  Hex:$DDCCBBAA
  $AA
  $BB
  $CC
  $DD
  
  







 
Related Info: LSL32 | LSR32 | Operators | PeekInt | ROL32 | ROR32 | SwapLowBytes | SwapWordOrder :
 


(c) Copyright 2002 - 2024 - Kevin Picone - PlayBASIC.com