잘 작동하는지 모름, 걍 밑바닥에서 부터 작성함
$ cat hdlc_nrzi_enc.c #include <stdio.h> void main() { unsigned char data[10] = {0x7E, 0x11, 0x22, 0x33, 0x1E, 0x0A, 0x7E, 0x00, 0x00, 0x00}; unsigned char lsbmsb[10] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; unsigned char bitstuff[10] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; unsigned char nrzi[10] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; int i = 0; int b = 0; // msb to lsb, data order change printf("bitwise LSB convert ----------------------------------------\n"); for(i = 0; i < 10; i++) { unsigned char tempval = 0; unsigned char orival = 0; orival = data[i]; for(b = 0; b < 8; b++) { tempval |= ((orival & 0x01) << (7 - b)); orival = orival >> 1; } lsbmsb[i] = tempval; printf("%02X -> %02X\n",data[i],lsbmsb[i]); } // zrzi printf("NRZ-I ----------------------------------------\n"); unsigned char last = 0; unsigned char out = 0; for(i = 0; i < 80; i++) { last = (lsbmsb[i / 8] >> (7 - (i % 8))) & 0x01; // printf("i:%d l:%d ", i, last); if(last == 0) out=(out==0?1:0); // printf("o:%d\n", out); nrzi[i / 8] |= out << (7 - (i % 8)); if(i % 8 == 7) printf("%02X -> %02X\n", lsbmsb[i/8],nrzi[i/8]); } } |
$ ./enc bitwise LSB convert ---------------------------------------- 7E -> 7E 11 -> 88 22 -> 44 33 -> CC 1E -> 78 0A -> 50 7E -> 7E 00 -> 00 00 -> 00 00 -> 00 NRZ-I ---------------------------------------- 7E -> FE 88 -> 5A 44 -> D2 CC -> 22 78 -> FA 50 -> CA 7E -> FE 00 -> AA 00 -> AA 00 -> AA |
$ cat hdlc_nrzi_dec.c #include <stdio.h> void main() { unsigned char data[10] = {0xFE, 0x5A, 0xD2, 0x22, 0xFA, 0xCA, 0xFE, 0x00, 0x00, 0x00}; unsigned char lsbmsb[10] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; unsigned char bitstuff[10] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; unsigned char nrzi[10] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; int i = 0; int b = 0; // zrzi printf("NRZ-I ----------------------------------------\n"); unsigned char last = 0; unsigned char curr = 0; unsigned char out = 0; for(i = 0; i < 80; i++) { curr = (data[i / 8] >> (7 - (i % 8))) & 0x01; // printf("i:%d l:%d c:%d ", i, last, curr); if(last != curr) out = 0; else out = 1 ; last = curr; // printf("o:%d\n", out); nrzi[i / 8] |= out << (7 - (i % 8)); if(i % 8 == 7) printf("%02X -> %02X\n", data[i/8],nrzi[i/8]); } // msb to lsb, data order change printf("bitwise LSB convert ----------------------------------------\n"); for(i = 0; i < 10; i++) { unsigned char tempval = 0; unsigned char orival = 0; orival = nrzi[i]; for(b = 0; b < 8; b++) { tempval |= ((orival & 0x01) << (7 - b)); orival = orival >> 1; } lsbmsb[i] = tempval; printf("%02X -> %02X\n",nrzi[i],lsbmsb[i]); } } |
$ ./dec NRZ-I ---------------------------------------- FE -> 7E 5A -> 88 D2 -> 44 22 -> CC FA -> 78 CA -> 50 FE -> 7E 00 -> FF 00 -> FF 00 -> FF bitwise LSB convert ---------------------------------------- 7E -> 7E 88 -> 11 44 -> 22 CC -> 33 78 -> 1E 50 -> 0A 7E -> 7E FF -> FF FF -> FF FF -> FF |
'모종의 음모 > HDLC' 카테고리의 다른 글
HDLC bit stuff (0) | 2022.08.29 |
---|---|
hdlc 프로토콜 - bit stuffing, endian? (0) | 2022.08.26 |
HDLC FCS ccitt-1 16bit crc (0) | 2021.12.06 |
HDLC 프로토콜(High level Data Link Control) (0) | 2021.07.27 |