Upending a Network value
Posted: 27 January 2010, 11:14 AM
I need to convert a big-endian network value to little-endian Unsigned Long. Does anyone have code for this?[/i]
A place to discuss ZBasic and ZX series microcontrollers.
https://forum.zbasic.net/
The code below can be used for swapping the byte order when changing from network order to host order and vice versa.dlh wrote:Does anyone have code for this?
Code: Select all
'
'' revByteOrder
'
' This function reverses the order of the bytes of a 32-bit value.
'
Function revByteOrder(ByVal val as UnsignedLong) as UnsignedLong
' define some based arrays for accessing the constituent bytes
Dim valBytesIn() as Byte Based val.DataAddress
Dim valBytesOut() as Byte Based revByteOrder.DataAddress
' reverse the byte order
revByteOrder = CULng(valBytesIn(4))
valBytesOut(2) = valBytesIn(3)
valBytesOut(3) = valBytesIn(2)
valBytesOut(4) = valBytesIn(1)
End Function
Code: Select all
#pragma warning(push; 2:Off) ' turn off warning: return value not set
Function revByteOrder(ByVal val as UnsignedLong) as UnsignedLong
' define some based arrays for accessing the constituent bytes
Dim valBytesIn() as Byte Based val.DataAddress
Dim valBytesOut() as Byte Based revByteOrder.DataAddress
' reverse the byte order
valBytesOut(1) = valBytesIn(4)
valBytesOut(2) = valBytesIn(3)
valBytesOut(3) = valBytesIn(2)
valBytesOut(4) = valBytesIn(1)
End Function
#pragma warning(pop)