왜 Hex두개를 합쳐야 할까

하나의 패킷에 자료 1~5까지의 데이터가 담겨서 통신을 해야한다. 이때 자료 4는 16비트가 필요하고 나머지는 8비트만 있어도 충분하다. 이런경우 자료 4를 8비트 두개로 쪼개서 보내는 경우가 있다.

쪼개는 방법이야 다양하겠지만 자료4의 값이 0x1234라면 0x12와 0x34로 쪼개서 보내는 경우가 경험상 많았다. 그래서 자료를 받는 쪽에서는 0x12와 0x34를 다시 0x1234로 만들어야 한다.
Hex 두개 합치기
- 0x12의 뒤에 0 8개를 붙여서 16비트로 만든다.
- 16비트로 만들어진 0x1200에 0x0034를 or연산한다.
#include <stdio.h> int convert(unsigned char low, unsigned char high) { int combined = high << 8; combined = combined | low; return combined; } int main() { unsigned char low = 0x12; unsigned char high = 0x48; printf("hex: %x\n", convert(low, high)); // hex: 4812 printf("int: %d\n", convert(low, high)); // int: 18450 return 0; }