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.
Write a C program to convert little Endian to Big Endian.
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);
}
printf("|\n");
/* 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);
}
printf("|\n");
return 0;
}
Output:
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|
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