-
Notifications
You must be signed in to change notification settings - Fork 0
/
endian_test.c
37 lines (26 loc) · 974 Bytes
/
endian_test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <stdio.h>
unsigned int ChangeEndianness(unsigned int value);
int main(void) {
union {
unsigned int word; // a 32-bit integer
unsigned char bytes[4];
} u;
u.word = 0x0A0B0C0D;
for (int i = 0; i < 4; i++) {
printf("byte[%d] = 0x%x\n", i, (int)u.bytes[i]);
}
printf("\n");
u.word = ChangeEndianness(u.word);
for (int i = 0; i < 4; i++) {
printf("byte[%d] = 0x%x\n", i, (int)u.bytes[i]);
}
}
unsigned int ChangeEndianness(unsigned int value)
{
unsigned int result = 0;
result |= (value & 0x000000FF) << 24;
result |= (value & 0x0000FF00) << 8;
result |= (value & 0x00FF0000) >> 8;
result |= (value & 0xFF000000) >> 24;
return result;
}