How to display nodejs raw Buffer data as Hex string

前端 未结 2 1690
自闭症患者
自闭症患者 2020-12-07 19:28

The following code uses SerialPort module to listen to data from a bluetooth connection.

I am expecting to see a stream of data in Hexadecimal format printed in cons

2条回答
  •  [愿得一人]
    2020-12-07 20:07

    Top answer is the simplest way to do it.

    An alternative method:

    data = Buffer.from([0x62, 0x75, 0x66, 0x66, 0x65, 0x72]);
    
    Array.prototype.map.call(new Uint8Array(data),
                   x => ('00' + x.toString(16)).slice(-2))
            .join('').match(/[a-fA-F0-9]{2}/g).reverse().join('');
    

提交回复
热议问题