0

I'm trying to make a text box which will give an output of a string in hex. There is a similar question here which I got my current code from which I modified a bit, but when I logged it, instead of the string "486578", I got "Hex". Here is my code:

function Hex() {
  var Hex = "Hex"
  Hex = Hex.toString('16');
  console.log(Hex)
}

Hex();

And no, I don't want something like encrypt.js. How would I solve this? Thanks!

ceving
  • 21,900
  • 13
  • 104
  • 178
Westlenando
  • 88
  • 1
  • 8

1 Answers1

0

You are not calling Number.prototype.toString() but String.prototype.toString().

Your input is the string "Hex". This gets converted to a string "Hex". You should try to use a number, if you want to convert a number.

In order to get numbers, you have to encode your string.

var encoded = new TextEncoder().encode("Hex");

var hex = Object.values(encoded).map(n => n.toString(16));

console.log (hex.join (' '));

Compare it with the ASCII table.

ceving
  • 21,900
  • 13
  • 104
  • 178