Convert hexadecimal number into decimal in JS & Reactjs – Code Example

Total
0
Shares

To convert a hexadecimal number into decimal in javascript and reactjs, you can use parseInt() function with base 16. It will take care of provided data if it is in string form too. Check this code example –

function hexToDecimal(x) {
  const parsed = parseInt(x, 16);
  if (isNaN(parsed)) { return 0; }
  return parsed;
}

console.log(hexToDecimal(' 0xFA50C'));
// expected output: 1025292

Source: MSDN