Swap bytes in UInt16

Hi,

I use GPIO module in Pi
Protected Function I2CReadReg16(fd As Integer, reg As Integer) As Int16.

It works fine but returns bytes in reverse order.

What is best way to swap bytes and return UInt16 from that?

Jukka

Memory block with big and little endian manipulated? Not in front of a computer so can’t check on it right at the moment.

I tryed something like this:

dim mb As new MemoryBlock(2) mb.LittleEndian = False mb.Int16Value(0) = i Return Hex(mb.UInt16Value(0))
But it didn’t work…

This is how I do it now and it works but there must be easier way:

[code] dim ti As String
Select case Hex(i).Len
case 0
ti = “0000”
case 1
ti = “000” + Hex(i)
case 2
ti = “00” + Hex(i)
case 3
ti = “0” + Hex(i)
case 4
ti = Hex(i)
End Select

Return “&h” + Right(ti,2) + Left(ti,2)[/code]

Try this:

dim mb as new MemoryBlock( 2 )
mb.UInt16Value( 0 ) = i
mb.LittleEndian = not mb.LittleEndian
i = mb.UInt16Value( 0 )

(If this is what you need, please mark Langue’s response as the “answer” since this is what he meant.)

Thank You!

I was wondering why it return bytes in reverse order, but reading LittleEndian from wikipedia and now I can understand.
World isn’t so black and white :slight_smile: