Anche se non è una risposta alla domanda effettiva, è forse utile in questo caso anche sapere come invertire il processo:
function bin2hex (bin)
{
var i = 0, l = bin.length, chr, hex = ''
for (i; i < l; ++i)
{
chr = bin.charCodeAt(i).toString(16)
hex += chr.length < 2 ? '0' + chr : chr
}
return hex
}
A titolo di esempio, utilizzando hex2bin
su b637eb9146e84cb79f6d981ac9463de1
rendimenti ¶7ëFèL·mÉF=á
, e poi passando questo a bin2hex
restituisce b637eb9146e84cb79f6d981ac9463de1
.
Potrebbe anche essere utile al prototipo queste funzioni all'oggetto String
:
String.prototype.hex2bin = function()
{
var i = 0, l = this.length - 1, bytes = []
for (i; i < l; i += 2)
{
bytes.push(parseInt(this.substr(i, 2), 16))
}
return String.fromCharCode.apply(String, bytes)
}
String.prototype.bin2hex = function()
{
var i = 0, l = this.length, chr, hex = ''
for (i; i < l; ++i)
{
chr = this.charCodeAt(i).toString(16)
hex += chr.length < 2 ? '0' + chr : chr
}
return hex
}
alert('b637eb9146e84cb79f6d981ac9463de1'.hex2bin().bin2hex())
@MartyIX: Ecco una [funzione JavaScript con licenza BSD] (http://freebeer.smithii.com/www/_source.php?file=%2Fhome%2Fross%2Fpublic_html%2Ffreebeer%2Fwww%2Flib%2Fbin2hex. js) fa quello che vuoi. –
Andres Rifrio: Grazie! –