-1

I am converting hex to hex strings then came across 2 ways to do it using the toString() method:

  • toString('hex')
  • toString(16)

I see a lot of resources online also use toString('hex') but when I tried to use it, it gives out an error message: RangeError: toString() radix argument must be between 2 and 36. The toString(16) works perfect.

But just for curiousity, what is the story behind this? Is the toString('hex') deprecated of some sort? Or is there any specific use-case when that is used instead of toString(16)

Example:

let num = 0x7

console.log('Convert using toSting(hex): ' + num.toString('hex')) // throws an error
console.log('Convert using toSting(16): ' + num.toString(16))
kzaiwo
  • 1,558
  • 1
  • 16
  • 45
  • 3
    I've never seen `toString('hex')`, it's invalid. I don't think it's ever been valid. Whatever resource you used is probably just really low quality or AI generated or something – mousetail Feb 10 '23 at 08:05
  • *Where* did you come across a sample using “hex”‽ – deceze Feb 10 '23 at 08:06
  • As others already pointed out only valid argument is An integer in the range 2 through 36 specifying the base to use for representing the number value. Defaults to 10. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toString – Jan Pfeifer Feb 10 '23 at 08:08
  • 8
    The `something.toString('hex')` applies when `something` is a [`Buffer`](https://nodejs.org/api/buffer.html) in node.js – Gabriele Petrioli Feb 10 '23 at 08:08
  • 1
    buffer.prototype.toString usage: `buffer.toString(encoding, start, end);` encoding - Optional. The encoding to use in the return value. Default 'utf8' – Nikola Diklich Feb 10 '23 at 08:16
  • 1
    @GabrielePetrioli This is a valid answer. And it definitely clarifies my confusion. If we can re-open this post, please post this as an answer and I will accept. Thank you!! – kzaiwo Feb 13 '23 at 02:30
  • 1
    I believe reopening this question and posting a valid answer will be beneficial to the community. The question is a valid question and there is a valid answer also (see @GabrielePetrioli's answer) – kzaiwo Feb 28 '23 at 10:01

1 Answers1

1

.toString(16) is how you convert Numbers to a hex string, .toString('hex') is how you convert NodeJS's Buffers to a hex string.

For Numbers, radix is an optional parameter, which should be an integer from 2 to 36, with a default value of 10.

toString([radix])

For Buffers, encoding is an optional parameter (with start and end also optional), which can be many values (also see this answer) such as "ascii", "utf8", and, in this case, "hex", with the default value of "utf8".

toString([encoding[, start[, end]]])

If you mix-match these, Number will throw a RangeError (toString() radix argument must be between 2 and 36) and Buffer will throw a TypeError (Unknown encoding: 16)

Samathingamajig
  • 11,839
  • 3
  • 12
  • 34
  • Thanks for reopening this question and posting a valid answer! I'm sure this would help a lot of people in the future! – kzaiwo Mar 06 '23 at 10:27