1

In JavaScript, how do I convert a string representation of a hexadecimal value into its hex representation?

I have a string value, "FE", returning from a checksum routine. I need its hexadecimal representation, "\xFE".

I cannot simply do this, as it gives me an error:

var crc = "FE";
var hex = "\x" + crc;

This just gives me a new four-character ASCII string:

var crc = "FE";
var hex = "0x" + "FE";
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Bruiser
  • 11
  • 2
  • I think you have to use [`parseInt`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt) – hindmost Oct 10 '14 at 14:22
  • 1
    It's not clear what you want. Do you want the string "\xFE"? Or an int having a value of 0xFE? – michaelgulak Oct 10 '14 at 14:24
  • possible duplicate of [How to convert decimal to hex in JavaScript?](http://stackoverflow.com/questions/57803/how-to-convert-decimal-to-hex-in-javascript) – Ben Oct 10 '14 at 14:25
  • `'FE'` *already is* is the hex representation of a number. What exactly do you want now? – Felix Kling Oct 10 '14 at 14:28
  • Maybe this `"FE" -> "\u00FE" -> "þ"` ? – Andreas Louv Oct 10 '14 at 14:34
  • I ended up figuring it out: var crc = "FE"; var hex = String.fromCharCode(parseInt(crc),16)); – Bruiser Oct 10 '14 at 15:00
  • Wow, I would never have guessed that that's what you want, given the problem description. Care to update your question and make it clearer? – Felix Kling Oct 11 '14 at 01:01

3 Answers3

3

Like this:

var hex = parseInt("FF", 16);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ben
  • 3,989
  • 9
  • 48
  • 84
0

For the string \xFE, escape the backslash: var hex = '\\x'+'FE'

To convert 'FE' to a number, use +('0xFE')

To show +('0xFE') as a hexadecimal, use (224).toString(16), or '0x'+((254).toString(16))

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
KooiInc
  • 119,216
  • 31
  • 141
  • 177
  • or `(123).toString(16)` or `var a = 123; a.toString(16)`. While `123.toString` will throw an error because the compiler will think you are about to write a float instead of an int. This will also work: `123 .toString` (Notice the space) – Andreas Louv Oct 10 '14 at 14:39
0

I was having the same problem. I had a string, let's say "CA030101CF", and I wanted to convert it to hexadecimal values, like 0xCA 0x03 0x01 0x01 0xCF. To be clear, not a string looking like hexadecimal values. It means 0xCA, not '0xCA' and so on.

Finally, the solution that worked for me was using the Node.js Buffer, combined with a function that would separate the string by two characters and adding a '0x' prefix.

Here are the two ways to do it.

// Solution 1 - direct string to buffer conversion
const hexToString = (hex) => {
  let ar = [];
  for (let c = 0; c < hex.length; c += 2) {
    ar.push('0x' + hex.substr(c, 2));
  }
  return ar;
};

let s = "CA030101CF";
let buf = Buffer.from(hexToString(s));
console.log(buf);
// Solution 2 - string to buffer conversion
const hexToInt = (hex) => {
  let ar = [];
  for (let c = 0; c < hex.length; c += 2) {
    ar.push(parseInt('0x' + hex.substr(c, 2), 16));
  }

  return ar;
};

let s = "CA030101CF";
let buf = Buffer.from(hexToInt(s));
console.log(buf);

I haven't checked yet which one is more efficient, but both solutions produce same result - a buffer of hexadecimal values <Buffer ca 03 01 01 cf>.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131