Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert an int to a little endian byte array?

I have this function in C# to convert a little endian byte array to an integer number:

int LE2INT(byte[] data) {   return (data[3] << 24) | (data[2] << 16) | (data[1] << 8) | data[0]; } 

Now I want to convert it back to little endian.. Something like

byte[] INT2LE(int data) {   // ... } 

Any idea?

Thanks.

like image 554
Yana D. Nugraha Avatar asked Feb 28 '10 04:02

Yana D. Nugraha


People also ask

How is an int stored in little endian?

On little endian platforms, the value 1 is stored in one byte as 01 (the same as big endian), in two bytes as 01 00, and in four bytes as 01 00 00 00. If an integer is negative, the "two's complement" representation is used. The high-order bit of the most significant byte of the integer will be set on.

How do you convert int to Bytearray?

The Ints class also has a toByteArray() method that can be used to convert an int value to a byte array: byte[] bytes = Ints. toByteArray(value);

How do you convert int to byte in Python?

An int value can be converted into bytes by using the method int. to_bytes(). The method is invoked on an int value, is not supported by Python 2 (requires minimum Python3) for execution.

How are arrays stored in memory Little endian?

In little-endian, the bytes are stored in the order least significant to most signficant. Big-endian is the opposite. For instance, short x=0x1234 would be stored as 0x34 , 0x12 in little-endian.


2 Answers

The BitConverter class can be used for this, and of course, it can also be used on both little and big endian systems.

Of course, you'll have to keep track of the endianness of your data. For communications for instance, this would be defined in your protocol.

You can then use the BitConverter class to convert a data type into a byte array and vice versa, and then use the IsLittleEndian flag to see if you need to convert it on your system or not.

The IsLittleEndian flag will tell you the endianness of the system, so you can use it as follows:

This is from the MSDN page on the BitConverter class.

  int value = 12345678; //your value   //Your value in bytes... in your system's endianness (let's say: little endian)   byte[] bytes = BitConverter.GetBytes(value);   //Then, if we need big endian for our protocol for instance,   //Just check if you need to convert it or not:   if (BitConverter.IsLittleEndian)      Array.Reverse(bytes); //reverse it so we get big endian. 

You can find the full article here.

Hope this helps anyone coming here :)

like image 80
Wracky Avatar answered Oct 05 '22 14:10

Wracky


Just reverse it, Note that this this code (like the other) works only on a little Endian machine. (edit - that was wrong, since this code returns LE by definition)

  byte[] INT2LE(int data)   {      byte[] b = new byte[4];      b[0] = (byte)data;      b[1] = (byte)(((uint)data >> 8) & 0xFF);      b[2] = (byte)(((uint)data >> 16) & 0xFF);      b[3] = (byte)(((uint)data >> 24) & 0xFF);      return b;   } 
like image 30
John Knoeller Avatar answered Oct 05 '22 13:10

John Knoeller