Skip to content

Instantly share code, notes, and snippets.

@shd101wyy
Last active June 7, 2023 01:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shd101wyy/b49498a96715cf18b2776cc7ff8c4d95 to your computer and use it in GitHub Desktop.
Save shd101wyy/b49498a96715cf18b2776cc7ff8c4d95 to your computer and use it in GitHub Desktop.
string to hex, hex to string
// https://stackoverflow.com/questions/21647928/javascript-unicode-string-to-hex?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa
String.prototype.hexDecode = function(){
var j;
var hexes = this.match(/.{1,4}/g) || [];
var back = "";
for(j = 0; j<hexes.length; j++) {
back += String.fromCharCode(parseInt(hexes[j], 16));
}
return back;
}
String.prototype.hexEncode = function(){
var hex, i;
var result = "";
for (i=0; i<this.length; i++) {
hex = this.charCodeAt(i).toString(16);
result += ("000"+hex).slice(-4);
}
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment