Server/개발

[Node.js] HEX array parsing

sangjun-pro 2022. 4. 14. 17:04

1. HEX + ASCII 데이터를 HEX로 파싱

 

이전 소스는 ( https://sangjunui.tistory.com/29?category=1007817 ) 참고

 

- 송신 데이터

tx_buffer[tx_len++] = (char)0x02;
tx_buffer[tx_len++] = '2';
tx_buffer[tx_len++] = '3';
tx_buffer[tx_len++] = '4';
tx_buffer[tx_len++] = '5';
tx_buffer[tx_len++] = '6';
tx_buffer[tx_len++] = '7';
tx_buffer[tx_len++] = '8';
tx_buffer[tx_len++] = '9';
tx_buffer[tx_len++] = '0';
tx_buffer[tx_len++] = 'a';
tx_buffer[tx_len++] = 'b';
tx_buffer[tx_len++] = 'd';
tx_buffer[tx_len++] = (char)0x03;
tx_buffer[tx_len++] = (char)0x13;

- 수신

var recv_buf = Buffer.from(data,'hex');
	
console.log(data.toString());
console.log(data.toString('hex'));
for(var dl = 0; dl < data.length; dl++)
{
    console.log("["+dl+"]" + recv_buf[dl]);
}

 

- 결과

234567890abd // utf8 출력
023233343536373839306162640313 // hex 출력
[0]2   // 0x02
[1]50  // 0x32 -> '0'
[2]51
[3]52
[4]53
[5]54
[6]55
[7]56
[8]57
[9]48
[10]97
[11]98
[12]100 //  0x62 'd'
[13]3   // 0x03
[14]19  // 0x13 'CR'