This blog is under construction

Saturday, 6 July 2013

C program to convert Little Endian to Big Endian - Endianness

Please check the below link find detailed information on Little Endian and Big Endian (Endianness).

Write a C program to convert little Endian to Big Endian.

  #include <stdio.h>
  int main () {
        int num, i, val;
        char *ptr;

        /* get the integer input from user */
        printf("Enter your input:");
        scanf("%d", &num);
        ptr = (char *)(&num);

        printf("Hexadecimal value of %d is 0x%x\n", num, num);

        /* writing byte by byte to get little endian format */
        printf("Little Indian Format: ");
        for (i = sizeof(int) - 1; i >= 0; i--) {
                val = *(ptr + i);
                printf("|0x%x", val);

        /* Writing byte by byte to get big endian format */
        printf("Big Indian Format   : ");
        for (i = 0; i < sizeof(int); i++) {
                val = *(ptr + i);
                printf("|0x%x", val);
        return 0;

  jp@jp-VirtualBox:~/$ ./a.out
  Enter your input:12345
  Hexadecimal value of 12345 is 0x3039
  Little Indian Format: |0x39|0x30|0x0|0x0|
  Big Indian Format   : |0x0|0x0|0x30|0x39|

No comments:

Post a Comment