Search code examples
javascripthex

How to get hex integer from a string in JS?


I would like to convert this: "#FFFFFF" to this: 0xFFFFFF. How is it possible without using eval?

Thanks in advance,


Solution

  • Strip off the "#" and use parseInt().

    var hex = parseInt(str.replace(/^#/, ''), 16);
    

    Then, if you want to see it in hex, you can use .toString():

    console.log(hex.toString(16));