Created
April 10, 2015 16:14
-
-
Save nicolsc/a3a55737ece5ae34124a to your computer and use it in GitHub Desktop.
JS hex/ascii
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
doesn't work when you try to convert with zeros. try hex2a('asdf000000asdf00'), it fails