Last active
July 1, 2022 21:20
-
-
Save kichMan/beda9bf944fba6d57d42f8f94984be02 to your computer and use it in GitHub Desktop.
JavaScript HEX to ASCII
This file contains 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
/** | |
* HEX to ASCII | |
*/ | |
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; | |
} | |
hex2a('32343630'); // returns '2460' | |
/** | |
* ASCII to HEX | |
*/ | |
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(''); | |
} | |
a2hex('2460'); //returns 32343630 | |
/** | |
* Number to HEX | |
*/ | |
(32).toString(16); | |
/** | |
* HEX to Number | |
*/ | |
parseInt('0x08', 10); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment