Search code examples
javascriptstringcharhexascii

How do I convert a ASCII hex string (ex. "6C" to "l") in javascipt?


How do I convert an ASCII hex string into a character?

Examples:

6E - n
26 - &
45 - E

I have searched on the internet but only found answers for the decimal system, not the double-hex format.


Solution

  • Use a combination of parseInt() and String.fromCharCode():

    const hex2Char = hex => String.fromCharCode(parseInt(hex, 16));
    
    console.log(hex2Char('6E'));
    console.log(hex2Char('26'));
    console.log(hex2Char('45'));