Sometimes, we want to convert from Hex to ASCII in JavaScript.
In this article, we’ll look at how to convert from Hex to ASCII in JavaScript.
How to convert from Hex to ASCII in JavaScript?
To convert from Hex to ASCII in JavaScript, we can use some array and string methods.
For instance, we write
const asciiVal = "32343630"
.match(/.{1,2}/g)
.map((v) => {
return String.fromCharCode(parseInt(v, 16));
})
.join("");
console.log(asciiVal);
to call the string match
method to match groups of 2 digits and return an array with the matches.
Then we call map
with a callback that gets the character code from the decimal number parsed from the hex number.
And then we call join
to combine the array of characters into a string.
Conclusion
To convert from Hex to ASCII in JavaScript, we can use some array and string methods.