0

I have a websocket proxy to TCP setup. But the data I receive is HEX buffer. How I can I convert it to string readable format? I think I have to set it to utf-8 but I don't see a option for that in websocket.

Example of data recevied:

Received:  <Buffer 3c 63 72 6f 73 73 2d 61 69 6e 272 6f  2a 27 ... 46 more bytes>

Client code:

const ws = new WebSocket('ws://example.com:1211');

ws.onmessage = message => {
  console.log('Received: ', message.data)
};
user630702
  • 2,529
  • 5
  • 35
  • 98

1 Answers1

0

Try this:


const convert = (from, to) => hexMessage => Buffer.from(hexMessage, from).toString(to);

const hexToUtf8 = convert('hex', 'utf8');

hexToUtf8('your hex msg here')

Also check out this post: Hex to String & String to Hex conversion in nodejs

Algo7
  • 2,122
  • 1
  • 8
  • 19
  • 1
    Thanks that worked. I used the Buffer from directly. `var buf = Buffer.from(message.data); console.log(buf.toString());` – user630702 Apr 23 '20 at 09:37