1

I want to convert a node JS buffer to a hex string. Obviously, I googled this first, but none of the proposed solutions work.

For example, in this question a comment below the accepted answer says you should do it like this:

let signature4 = Buffer.from(signature3.r, 'hex') + Buffer.from(signature3.s, 'hex') + Buffer.from(signature3.v, 'hex');

But this yields:

TypeError [ERR_INVALID_ARG_TYPE]: The "value" argument must not be of type number. Received type number

If I go for the actual answer which tells me to do it like this:

let signature4 = signature3.r.toString('hex') + signature3.s.toString('hex') + signature3.v.toString('hex');

I get this error:

 RangeError: toString() radix argument must be between 2 and 36

If I follow the advice given in the error message and enter 16 as a number like so:

let signature4 = signature3.r.toString(16) + signature3.s.toString(16) + signature3.v.toString(16);

I get this error message:

 TypeError [ERR_UNKNOWN_ENCODING]: Unknown encoding: 16

If I instead pass 16 as a string:

let signature4 = signature3.r.toString('16') + signature3.s.toString('16') + signature3.v.toString('16');

I get the same error message:

 TypeError [ERR_UNKNOWN_ENCODING]: Unknown encoding: 16

So what's the current way of doing it?

I use Node v10.18.1.

UTF-8
  • 575
  • 1
  • 5
  • 23

1 Answers1

2

This works for me... is your object actually an Node.js buffer?

Buffer.from([255,254,0,1]).toString("hex") // ffee0001
Cody G
  • 8,368
  • 2
  • 35
  • 50
  • Damn, I feel stupid now. Shouldn't have written all of that in one line. The `r` and `s` values are buffers but `v` is not. `v` is a number. So I need to pass `'hex'` to `toString()` of `r` and `s` but I need to pass `16` to `toString` of `v`. – UTF-8 Jan 21 '20 at 15:14
  • Happens to the best of us :) – Cody G Jan 21 '20 at 15:24