-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
67 lines (50 loc) · 1.59 KB
/
main.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
Project 10
#HowToConvertFromLittleToBigEndian
Description
The program converts an integer from one to another system
(Big Endian <-> Little Endian) using bitwise operations.
Big-endian systems store the Most Significant Byte (MSB)
of a word First.
A little-endian system, in contrast, stores the Least Significant
Byte (LSB) First.
For Easy reference:
BIG ENDIAN (Most First) LITTLE ENDIAN (Least First)
0A 0B 0C 0D 0A 0B 0C 0D
0A _ _| | | | | | | |_ _ 0D
0B _ _ _| | | | | |_ _ _ 0C
0C _ _ _ _ | | | | _ _ _ _ 0B
0D _ _ _ _ _ | | _ _ _ _ _ 0A
*****************************
Output
little_endian = a0b0c0d
big_endian = d0c0b0a
*****************************
Author: #TechVedasLearn #EmbeddedSystem #EndiannessInC
https://youtu.be/a9lVoThjV7o
Edited by j3
Date: Jun, 20/2020
*/
#include <stdio.h>
#define uint32_t int
/* Prototype */
uint32_t ChangeEndianness(uint32_t value);
int main()
{
uint32_t little_endian = 0x0A0B0C0D;
uint32_t big_endian = 0;
big_endian = ChangeEndianness(little_endian);
printf("little_endian = %x\n", little_endian);
printf("big_endian = %x\n", big_endian);
//system("pause");
return 0;
}
uint32_t ChangeEndianness(uint32_t value)
{
uint32_t result = 0;
result |= (value & 0x000000FF) << 24;
result |= (value & 0x0000FF00) << 8;
result |= (value & 0x00FF0000) >> 8;
result |= (value & 0xFF000000) >> 24;
return result;
}