Skip to content

Instantly share code, notes, and snippets.

@peteringram0
Created August 25, 2022 18:32
Show Gist options
  • Save peteringram0/7a0955392006bc7ca165dd10594607be to your computer and use it in GitHub Desktop.
Save peteringram0/7a0955392006bc7ca165dd10594607be to your computer and use it in GitHub Desktop.
hex to string and string to hex
/**
* 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