Skip to content

Instantly share code, notes, and snippets.

@haruo31
Created March 26, 2019 14:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save haruo31/bfa18dbfcf10514b010e12d2d71590f6 to your computer and use it in GitHub Desktop.
Save haruo31/bfa18dbfcf10514b010e12d2d71590f6 to your computer and use it in GitHub Desktop.
The script converts 8bit hex string into integer list for java bytes array

The script converts 8bit hex string into integer list for java bytes array

e.g.

  • 0x94, 0x0a, 0xc0 -> -0x6c, 0x0a, -0x40
  • 94 0A C0 -> -0x6c, 0x0a, -0x40

this script uses Array.prototype.map().

input

<textarea id="input"> </textarea>

GO

output

<textarea id="output"> </textarea> <script> function calc() { var input = document.getElementById("input").value; var out = document.getElementById("output"); var radix = input.match(/[a-f]|0x/i) ? 16 : 10; out.value = input.split(/[,\s]\s*/).map(function (nstr) { var n = parseInt(nstr, radix); if (n > 127) { n -= 256; } return n.toString(16); }).join(", ") .replace(/([0-9a-f]+)/gi, '0x$1') .replace(/0x([0-9a-f]),/gi, '0x0$1,') .replace(/(([^,]*,){10}) */g, '$1\n'); } </script>
@haruo31
Copy link
Author

haruo31 commented Mar 26, 2019

To use: export markdown to html.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment