Skip to content

Instantly share code, notes, and snippets.

@AsaoluElijah
Created January 31, 2020 14:23
Show Gist options
  • Save AsaoluElijah/a1df37900659e5cda9f0d85a8a1ba02d to your computer and use it in GitHub Desktop.
Save AsaoluElijah/a1df37900659e5cda9f0d85a8a1ba02d to your computer and use it in GitHub Desktop.
function clean_hex(input, remove_0x) {
input = input.toUpperCase();
if (remove_0x) {
input = input.replace(/0x/gi, "");
}
var orig_input = input;
input = input.replace(/[^A-Fa-f0-9]/g, "");
if (orig_input != input)
alert ("Warning! Non-hex characters (including newlines) in input string ignored.");
return input;
}
function Convert() {
var cleaned_hex = clean_hex(document.frmConvert.hex.value, document.frmConvert.chbSeparator.checked);
var filename = document.frmConvert.filename.value;
document.frmConvert.cleaned_hex.value = cleaned_hex;
if (cleaned_hex.length % 2) {
alert ("Error: cleaned hex string length is odd.");
return;
}
var binary = new Array();
for (var i=0; i<cleaned_hex.length/2; i++) {
var h = cleaned_hex.substr(i*2, 2);
binary[i] = parseInt(h,16);
}
var byteArray = new Uint8Array(binary);
var a = window.document.createElement('a');
a.href = window.URL.createObjectURL(new Blob([byteArray], { type: 'application/octet-stream' }));
a.download = filename;
// Append anchor to body.
document.body.appendChild(a)
a.click();
// Remove anchor from body
document.body.removeChild(a)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment