Skip to content

Instantly share code, notes, and snippets.

@nicolsc
Created April 10, 2015 16:14
Show Gist options
  • Save nicolsc/a3a55737ece5ae34124a to your computer and use it in GitHub Desktop.
Save nicolsc/a3a55737ece5ae34124a to your computer and use it in GitHub Desktop.
JS hex/ascii
//Thanks stackoverflow
//http://stackoverflow.com/questions/3745666/how-to-convert-from-hex-to-ascii-in-javascript
function a2hex(str) {
var arr = [];
for (var i = 0, l = str.length; i < l; i ++) {
var hex = Number(str.charCodeAt(i)).toString(16);
arr.push(hex);
}
return arr.join('');
}
function hex2a(hexx) {
var hex = hexx.toString();//force conversion
var str = '';
for (var i = 0; i < hex.length; i += 2)
str += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
return str;
}
@glenara
Copy link

glenara commented Jan 16, 2018

How do you convert (or what do you enter) in hex2a() to convert a hex values such as \u0153
Can it convert these higher hex values?
Thanks

@nesletC
Copy link

nesletC commented Jun 17, 2024

doesn't work when you try to convert with zeros. try hex2a('asdf000000asdf00'), it fails

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment