Little-Endian

Little-endian is an order in which the least significant byte of a numeric data type is stored first (at the lowest address). The rest of the bytes follow in increasing order of significance.

For example, the 32-bit integer 0x12345678 would be stored in memory as:

0x78 0x56 0x34 0x12

Java example - Convert int to 4 bytes:

1
2
3
4
5
6
int x = 0x12345678;

byte b0 = (byte)(x & 0xff); // 0x78
byte b1 = (byte)((x >> 8) & 0xff); // 0x56
byte b2 = (byte)((x >> 16) & 0xff); // 0x34 
byte b3 = (byte)((x >> 24) & 0xff); // 0x12

C++ example - Read integer from byte array:

1
2
3
4
5
6
7
8
uint32_t readInt(char bytes[4]) {
  uint32_t x = 0;
  x |= (uint32_t)bytes[0];
  x |= (uint32_t)bytes[1] << 8;
  x |= (uint32_t)bytes[2] << 16;
  x |= (uint32_t)bytes[3] << 24;
  return x;
}

Python example - Convert hex string:

1
2
3
hex_str = "78563412"
int_val = int(hex_str, 16) 
# 305419896 - bytes reversed

Little-endian ordering is common in hardware and networking for compactness. But care must be taken with multi-byte values.

little-endian is an order in which bytes are arranged in computer memory for representing data. In little-endian, the least significant byte is stored at the smallest memory address.

For example, the 32-bit integer 0x12345678 would be stored in memory as:

0x78 0x56 0x34 0x12

With least significant byte 0x78 at the smallest address. This is opposite to big-endian where the most significant byte is stored first.

little-endian is used in architectures like x86 and is the predominant byte order today. Programs may need to convert between endian formats.

Solution

Here is sample code to convert a 32-bit integer from big-endian to little-endian in C++:

1
2
3
4
5
6
int bigToLittleEndian(int x) {
  return (x & 0x000000FF) << 24 | 
         (x & 0x0000FF00) << 8 |
         (x & 0x00FF0000) >> 8 |
         (x & 0xFF000000) >> 24;
}

This shifts and combines the bytes in reverse order.

And to convert back from little-endian to big-endian:

1
2
3
int littleToBigEndian(int x) {
  return bigToLittleEndian(x); 
}

We reuse the same function. Endianness must be handled properly for data storage and transmission.

Description

Little-endian is a format for storage of binary data where the least significant byte (LSB) is stored first, at the lowest memory address. It is opposite to the big-endian format where the most significant byte (MSB) is stored first. When you read multi-byte data like integers or floats, you need to know the endianness to interpret the bytes correctly.

Solution

Let’s see how to detect little-endian and how to convert a 4-byte integer from little-endian to big-endian in different languages.

Java

Java’s ByteBuffer class allows you to specify endianness. Below is how you can work with little-endian integers.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

public class EndiannessExample {
    public static void main(String[] args) {
        ByteBuffer buffer = ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN);
        buffer.putInt(1);
        byte[] bytes = buffer.array();

        // Convert it back
        int intValue = ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).getInt();
        System.out.println("Integer Value: " + intValue);
    }
}

C++

In C++, you can use bit manipulation to convert between endianness.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
#include <iostream>
#include <cstdint>

uint32_t swap_endian(uint32_t val) {
    return ((val & 0xFF000000) >> 24) |
           ((val & 0x00FF0000) >> 8)  |
           ((val & 0x0000FF00) << 8)  |
           ((val & 0x000000FF) << 24);
}

int main() {
    uint32_t x = 1;
    uint32_t y = swap_endian(x);
    std::cout << "Converted Value: " << y << std::endl;
}

Python

In Python, the struct library can be used to work with little-endian integers.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
import struct

def to_little_endian(value):
    return struct.pack('<I', value)

def from_little_endian(byte_array):
    return struct.unpack('<I', byte_array)[0]

# Convert to little-endian bytes
byte_array = to_little_endian(1)
print("Byte array:", byte_array)

# Convert it back
value = from_little_endian(byte_array)
print("Integer Value:", value)

Key Takeaways

  • Little-endian stores the least significant byte at the lowest address.
  • Understanding endianness is crucial for correct data interpretation and manipulation.
  • Java’s ByteBuffer, C++ bit manipulation, and Python’s struct library provide ways to handle little-endian data.