STUDY
Convert Big to Little Endian, vice versa 본문
걸리버 여행기에 나오는 Big & Little Endians. 메모리에 data 를 저장하고 전송하는 두 가지 방법. Big endian 은 most significant bit 을 left-most memory address 에, 반대로 Little endian 은 right-most memory address 에 저장하는 방법. Big endian 에서는 high byte transferred fisrt, Little endian 에서는 low byte transferred first.
예를 들면, integer value인 1234 값이 있다고 해보자. 이는 hexadecimal value로 변환하면 4D2 이다. 1 byte는 8 bits, hexadecimal one digit 은 half byte (4 bits), hexadecimal two digits 는 one byte, int 는 4 bytes size, unsigned char 는 1 byte size. 따라서 1234 (int) = 0x000004D2 이고, Big or Little Endian 에 따라서 메모리에 0x00 0x00 0x04 0xD2 (Big Endian), 0xD2 0x04 0x00 0x00 (Little Endian)으로 기록된다.
#include
int isBigEndian();
void change_endian(int);
int main() {
/********************************************************
* check endianness of System
*******************************************************/
if (isBigEndian()) {
printf("This system is Big Endian.\n");
} else {
printf("This system is Little Endian.\n");
}
/********************************************************
* Intel x86-64 is little endian
*******************************************************/
unsigned char b[sizeof(int)];
int n = 1234; // hex 0x04D2
memcpy(b, &n, sizeof(int));
printf("%d\n", sizeof(b));
for (int i = 0; i < 4; i++) {
printf("%x ", b[i]);
} // d2 4 0 0
printf("\n");
/********************************************************
* another example
*******************************************************/
n = 1234;
const int arrayLength = sizeof(int);
unsigned char *bytePtr = (unsigned char *)&n;
for (int i = 0; i < arrayLength; i++) {
printf("[%X]", bytePtr[i]);
}
printf("\n");
/********************************************************
* change endian
*******************************************************/
n = 1234;
change_endian(n);
return 0;
}
/**********************************************************
* Andrew Ippoliti's Blog: Handling Endianness in C
* http://blog.acipo.com/handling-endianness-in-c/
* Test will be stored as [0x00,0x01] on a big endian machine
* and as [0x01, 0x00] on a little endian machine.
* (if int is 2 bytes)
*********************************************************/
int isBigEndian() {
int test = 1; // int is 4 bytes
char *p = (char *)&test; // char is 1 byte
return p[0] == 0;
}
/**********************************************************
* Convert Endian
*********************************************************/
void change_endian(int num) {
unsigned char *p = (unsigned char *)#
if (isBigEndian()) {
printf("Convert Big to Little Endian: ");
for (int i = 0; i < sizeof(int); i++) {
printf("%x ", *(p + i));
}
} else {
printf("Convert Little to Big Endian: ");
for (int i = sizeof(int) - 1; i >= 0; i--) {
printf("%x ", *(p + i));
}
}
printf("\n");
}
'c' 카테고리의 다른 글
Read `wav` file (0) | 2020.07.15 |
---|---|
gcc, play sound file, wave, mp3 (0) | 2020.06.27 |
Pointer to 2-D Character Array, Type Conversion (0) | 2020.04.02 |
raylib: GUI programming library (0) | 2020.02.18 |
MSYS2 `pacman -Syu` error (0) | 2020.02.18 |
Comments