Last active
May 23, 2025 13:12
-
-
Save shd101wyy/b49498a96715cf18b2776cc7ff8c4d95 to your computer and use it in GitHub Desktop.
string to hex, hex to string
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
// 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