C Program to change Endianness of data and bytes swapping example using Pointers

Data endianness is quite straightforward and at the same time critical part when we are talking about the order in which our data is going to be stored. By the word endianness, we mean the order in which each data bytes will be placed in the memory and if the next data byte will be placed either on the right of previous data byte or on the left. Talking about the types of data endianness, there are two each of them listed and explained here.

  • Big endianness
  • Little endianness

Big endianness Introduction

In big endianness, the most significant byte of the data will be stored at the least significant block of memory. Sounds too complex but once you get this it will be the easiest concept in memory management for you. For better and easier understanding lets’ take a simple example in which we will assume some data and store it in both big and little endianness.

Data = 0x11223344

This is a 32 bit data and we will first save it in big endian format.

C program to change data endianness bytes swapping program

In the assumed data 11 was the most significant byte and was placed at the least significant memory block i.e. the lowest address.

Little endianness

This format of data storing is the exact opposite of the one discussed earlier. In little endianness, the processor stores the least significant data byte of the word at the lowest numbered byte address, and the most significant byte at the highest numbered byte address. For further illustration see the figure below:

For the same data in the above example, the little-endian format will look like this.

C program to change data endianness bytes swapping program example

Since, 11 was the most significant byte, it is stored at the highest numbered byte address, and vice versa.

Why we need to Change Data Endianess?

You might be assuming why we need to change the data endianness. It may seem quite insignificant but it plays an important role when we are communicating between 2 machines. If the endianness of the host machines doesn’t match with the server the data you will receive at the host will be corrupted and not what you expected.

How to Change Data Endianess?

The easiest way to change the endianness of the data is through pointers. We simply create two pointers and change the most significant byte of the data pointer into the least significant byte of the second pointer. A sample code to do so is shown in the figure below:

C Program

This is a beginner level simple code to change the endianness of data. Let’s say, first data is store in big-endian format, then this code will change the data into little endianness format. However, it can also work the opposite way. Because changing data endianness is simply swapping the bytes order of data.

#include <stdio.h>
int main(void)
{
	int big_endian = 0x17645635;
	int little_endian;
	char *big_endian_ptr = (char *)&big_endian;
	char *little_endian_ptr = (char *)&little_endian;
	little_endian_ptr[0] = big_endian_ptr[3];
	little_endian_ptr[1] = big_endian_ptr[2];
	little_endian_ptr[2] = big_endian_ptr[1];
	little_endian_ptr[3] = big_endian_ptr[0];
	printf("Data in big endianness = 0x%x \nData in little endianness = 0x%x\n",big_endian,little_endian);

}

Initially, the data was stored in the big-endian format but we intend to change it to little-endian, for which we used the pointers. These pointers help us access the memory blocks of the stored data. Each pointer points to a single memory block of data and we store the last byte of big-endian data variable in the first byte of the little-endian data variable. The reverse of this function is quite straightforward and left for you as an assignment.

The output of the above program will display the assumed data in both formats. As shown in the figure below:

C program to change data endianness example code output

Second Method

In C programming, a common practice is to use function for each specific task. In this C program, function and pointers that points to original data are passed as a argument to the function. But this example peforms the same functionality.

#include <stdio.h>
void change_endianness(char *big_endian_ptr, char *little_endian_ptr)
{
	little_endian_ptr[0] = big_endian_ptr[3];
	little_endian_ptr[1] = big_endian_ptr[2];
	little_endian_ptr[2] = big_endian_ptr[1];
	little_endian_ptr[3] = big_endian_ptr[0];    
}
int main(void)
{
	int big_endian = 0x17645635;
	int little_endian;
	change_endianness((char *)&big_endian, (char *)&little_endian);
	printf("Data in big endianness = 0x%x \nData in little endianness = 0x%x\n",big_endian,little_endian);
}

Third Method

This example code perform data swapping operation on same variable instead of creating a new variable.

#include <stdio.h>
void swap_bytes(int *x)
{
    char temp_byte, i;
    for(i=0; i<2; i++)
    {
      temp_byte = *((char*)x+(3-i));
     *((char*)x+(3-i)) =  *((char*)x+(i));
      *((char*)x+(i)) =  temp_byte;
    }     
}
int main(void)
{
     int data = 0x17645635;
     int *ptr = &data;
     swap_bytes(ptr);
     printf("Data in big endianness = 0x%x",data);
}

Related Articles:

  1. C Program Compilation steps & Example with GCC Linux
  2. Introduction to Pointers
  3. How to find an array size without using sizeof()
  4. Pointer Arithmetic, Pointer Size, and Pointer Type
  5. In which order function parameters are evaluated in C/C++?

Leave a Reply