We need to know few things before knowing about Endianness.
What is a word?
1 word = 4 bytes.
What is a byte?
1 byte = 8 bits.
How the data are stored in memory?
Consider an integer num and it holds the value 12345. Size of an integer is 4 bytes. "num" is an integer and its size should be 4 bytes. The hexadecimal value of 12345 is 0x3039.
Writing 0x3039 in word format(Big Endian).
+-------------------------+
| 00 | 00 | 30 | 39 |
+-------------------------+
Writing 0x3039 in word format(Little Endian).
+-------------------------+
| 39 | 30 | 00 | 00 |
+-------------------------+
The data would be stored in the above format in memory.
Big Endian stores the Most significant byte at the smallest address.
Address Value
+------------------------------------+
| 0x45325 | 00 |
+------------------------------------+
| 0x45326 | 00 |
+------------------------------------+
| 0x45327 | 30 |
+------------------------------------+
| 0x45328 | 39 |
+------------------------------------+
Little Endian stores the Least Significant byte at the smallest address
Address Value
+------------------------------------+
| 0x45325 | 39 |
+------------------------------------+
| 0x45326 | 30 |
+------------------------------------+
| 0x45327 | 00 |
+------------------------------------+
| 0x45328 | 00 |
+------------------------------------+
What is a word?
1 word = 4 bytes.
What is a byte?
1 byte = 8 bits.
How the data are stored in memory?
Consider an integer num and it holds the value 12345. Size of an integer is 4 bytes. "num" is an integer and its size should be 4 bytes. The hexadecimal value of 12345 is 0x3039.
Writing 0x3039 in word format(Big Endian).
+-------------------------+
| 00 | 00 | 30 | 39 |
+-------------------------+
Writing 0x3039 in word format(Little Endian).
+-------------------------+
| 39 | 30 | 00 | 00 |
+-------------------------+
Big Endian stores the Most significant byte at the smallest address.
Address Value
+------------------------------------+
| 0x45325 | 00 |
+------------------------------------+
| 0x45326 | 00 |
+------------------------------------+
| 0x45327 | 30 |
+------------------------------------+
| 0x45328 | 39 |
+------------------------------------+
Little Endian stores the Least Significant byte at the smallest address
Address Value
+------------------------------------+
| 0x45325 | 39 |
+------------------------------------+
| 0x45326 | 30 |
+------------------------------------+
| 0x45327 | 00 |
+------------------------------------+
| 0x45328 | 00 |
+------------------------------------+
Write a C program to convert Big Endian to Little 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 big endian format */
printf("Big Indian Format : ");
for (i = 0; i < sizeof(int); i++) {
val = *(ptr + i);
printf("|0x%x", val);
}
printf("|\n");
/* 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");
return 0;
}
Output:
jp@jp-VirtualBox:~/$ ./a.out
Enter your input:12345
Hexadecimal value of 12345 is 0x3039
Big Indian Format : |0x0|0x0|0x30|0x39|
Little Indian Format: |0x39|0x30|0x0|0x0|
Enter your input:12345
Hexadecimal value of 12345 is 0x3039
Big Indian Format : |0x0|0x0|0x30|0x39|
Little Indian Format: |0x39|0x30|0x0|0x0|
No comments:
Post a Comment