5

I want to convert an number (integer) to a hex string

2 (0x02) to "\x02"

or

62 (0x0062) to "\x62"

How can I do that correctly?

DarkLeafyGreen
  • 69,338
  • 131
  • 383
  • 601

3 Answers3

15

You can use the to string method:

a = 64;
a.toString(16); // prints "40" which is the hex value
a.toString(8); // prints "100" which is the octal value
a.toString(2); // prints "1000000" which is the binary value
Ibu
  • 42,752
  • 13
  • 76
  • 103
4

Well, it's seems that you want just to concatenate the integer with \x.

If so just to like that:

var number = 62;
var hexStr = '\x' + number.toString(16);

But you have something strange about explaining.

Note: that 62 is not the same as 0x62, 0x62 would be 98.

volter9
  • 737
  • 5
  • 16
1

var converted = "\x" + number.toString(16)

Neel
  • 597
  • 6
  • 19