diff options
author | Simone Bortolin <simonebortolin@users.noreply.github.com> | 2022-08-10 00:16:15 +0200 |
---|---|---|
committer | Simone Bortolin <simonebortolin@users.noreply.github.com> | 2022-12-19 23:01:14 +0100 |
commit | d016d172f00d318003bfe380859c9b97e5be6802 (patch) | |
tree | 4f60055bcad2e1209827631ae7a78bfd2c7c646c /ascii-hex.md | |
parent | add mermaid (diff) | |
download | hack-gpon.github.io-d016d172f00d318003bfe380859c9b97e5be6802.tar hack-gpon.github.io-d016d172f00d318003bfe380859c9b97e5be6802.tar.gz hack-gpon.github.io-d016d172f00d318003bfe380859c9b97e5be6802.tar.bz2 hack-gpon.github.io-d016d172f00d318003bfe380859c9b97e5be6802.tar.lz hack-gpon.github.io-d016d172f00d318003bfe380859c9b97e5be6802.tar.xz hack-gpon.github.io-d016d172f00d318003bfe380859c9b97e5be6802.tar.zst hack-gpon.github.io-d016d172f00d318003bfe380859c9b97e5be6802.zip |
Diffstat (limited to 'ascii-hex.md')
-rw-r--r-- | ascii-hex.md | 19 |
1 files changed, 14 insertions, 5 deletions
diff --git a/ascii-hex.md b/ascii-hex.md index 116efce..6328076 100644 --- a/ascii-hex.md +++ b/ascii-hex.md @@ -9,12 +9,12 @@ description: Tool for converter ASCII and Hex <h1>ASCII To Hex</h1> <form id="ascii-to-hex"> <div class="form-floating mb-3"> - <input type="text" class="form-control" placeholder="ASCII" name="ascii-to-hex" id="ascii-to-hex" min="1000" max="10000"> + <input type="text" class="form-control" placeholder="ASCII" name="ascii-to-hex" id="ascii-to-hex" > <label for="ascii-to-hex">ASCII</label> </div> <div class="form-floating mb-3"> <input type="text" class="form-control" placeholder="Glue" name="ascii-to-hex-glue" id="ascii-to-hex-glue" value=" "> - <label for="ascii-to-hex-glue">Glue</label> + <label for="ascii-to-hex-glue">Glue/Separator (empty for the format 0x0123456789ABCDE, ` ` for the format 0x01 0x23 0x45 0x67 0x89 0xAB 0xCD 0xEF)</label> </div> <div class="mb-3"> <input type="submit" class="btn btn-primary" value="Calculate!"> @@ -32,7 +32,7 @@ description: Tool for converter ASCII and Hex </div> <div class="form-floating mb-3"> <input type="text" class="form-control" placeholder="Separator" name="hex-to-ascii-separator" id="hex-to-ascii-separator" value=" "> - <label for="hex-to-ascii-separator">Separator</label> + <label for="hex-to-ascii-separator">Glue/Separator (empty for the format 0x0123456789ABCDEF, ` ` for the format 0x01 0x23 0x45 0x67 0x89 0xAB 0xCD 0xEF)</label> </div> <div class="mb-3"> <input type="submit" class="btn btn-primary" value="Calculate!"> @@ -44,13 +44,22 @@ description: Tool for converter ASCII and Hex </form> <script> + + function getChunks(s, i) { + var a = []; + do{ a.push(s.substring(0, i)) } while( (s = s.substring(i)) != "" ); + return a; + } + var asciiToHexForm = document.getElementById('ascii-to-hex'); asciiToHexForm.addEventListener('submit',(event) => { event.preventDefault(); var fomrdata = new FormData(asciiToHexForm); var str = fomrdata.get('ascii-to-hex'); var glue = fomrdata.get('ascii-to-hex-glue'); - var hex = [...str].map((elem, n) => "0x"+Number(str.charCodeAt(n)).toString(16)).join(glue); + var prefixi = glue !== "" ? "0x" : ""; + var prefix = glue === "" ? "0x" : ""; + var hex = prefix + ([...str].map((elem, n) => prefixi+Number(str.charCodeAt(n)).toString(16)).join(glue)); document.getElementById('hex-result').value = hex; }); @@ -60,7 +69,7 @@ description: Tool for converter ASCII and Hex var fomrdata = new FormData(hexToAsciiForm); var str = fomrdata.get('hex-to-ascii'); var separator = fomrdata.get('hex-to-ascii-separator'); - var ascii = str.split(separator).map(el => String.fromCharCode(Number(el))).join(''); + var ascii = separator === "" ? getChunks(str.substring(2),2).map(el => String.fromCharCode(parseInt(el, 16))).join('') : str.split(separator).map(el => String.fromCharCode(Number(el))).join(''); document.getElementById('ascii-result').value = ascii; }); |