2

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.

1 Answer 1

3

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

Buffer.from([255,254,0,1]).toString("hex") // ffee0001
Sign up to request clarification or add additional context in comments.

2 Comments

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.
Happens to the best of us :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.