Spread the love
Write a C program to convert Little to Big endian integer
What is Endianess ?
Little and Big Endian are the ways of storing multi-byte data likes int, float, double etc.
Little Endian
In little endian machines, last byte of binary representation of the multi-byte data is stored first.
Big Endian
In big endian machines, first byte of binary representation of the multibyte data is stored first.
Code to convert Little endian integer to Big endianĀ
#include <stdio.h> unsigned int LitToBigEndian(unsigned int x); int main( ) { int Little_Endian = 0xAABBCCDD ; printf("\n Little_Endian = 0x%X\n",Little_Endian); printf("\n Big_Endian = 0x%X\n",LitToBigEndian(Little_Endian)); } unsigned int LitToBigEndian(unsigned int x) { return (((x>>24) & 0x000000ff) | ((x>>8) & 0x0000ff00) | ((x<<8) & 0x00ff0000) | ((x<<24) & 0xff000000)); }