Created
August 25, 2022 18:32
-
-
Save peteringram0/7a0955392006bc7ca165dd10594607be to your computer and use it in GitHub Desktop.
hex to string and string to hex
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
/** | |
* String to Hex | |
*/ | |
function stringToHex(str: Readonly<string>) { | |
let hex, i | |
let result = '' | |
for (i = 0; i < str.length; i++) { | |
hex = str.charCodeAt(i).toString(16) | |
result += hex | |
} | |
return result | |
} | |
/** | |
* Hex to string | |
*/ | |
function hexToString(_hex: Readonly<string>) { | |
const hex = _hex.toString() //force conversion | |
let str = '' | |
for (let 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