Created
May 9, 2018 21:58
-
-
Save zdila/476fbeff02eb40517fc7d4beeef8526a to your computer and use it in GitHub Desktop.
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
| // Tool for adding checksum and header (if missing) to uBlox UBX message. | |
| // by Martin Ždila <[email protected]> | |
| // License: CC0 | |
| const arg = process.argv[2]; | |
| if (!arg) { | |
| console.error(`Usage: process.argv[0] process.argv[1] AABBCCDD...`); | |
| process.exit(1); | |
| } | |
| const res = /^(b562)?(([0-9a-f]{2}){4,})$/i.exec(arg); | |
| if (!res) { | |
| console.error(`Invalid input. Use hexadecimal string, eg. 02010000`); | |
| process.exit(1); | |
| } | |
| console.log( | |
| // credits https://blog.tompawlak.org/split-string-tokens-defined-length-javascript | |
| 'b562' + res[2] + res[2].match(/.{1,2}(?=(.{2})+(?!.))|.{1,2}$/g) | |
| .map(x => parseInt(x, 16)) | |
| .reduce(([a, b], x) => ([a + x, b + a + x]), [0, 0]).map(n => (n % 256).toString(16).padStart(2, '0')) | |
| .join('') | |
| ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment