Last active
November 11, 2020 20:04
-
-
Save bmatusiak/4f7bec92ef6d4b60ea42b02483ea3589 to your computer and use it in GitHub Desktop.
cgode checksum cs *:
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
| // N0 M106*36 | |
| // N1 G28*18 | |
| // N2 M107*39 | |
| getCS("N0 M106"); | |
| getCS("N1 G28"); | |
| getCS("N2 M107"); | |
| function getCS(mystr) { | |
| //https://reprap.org/wiki/G-code#.2A:_Checksum | |
| var str = toUTF8Array(mystr); | |
| var cs = 0; | |
| for (var i = 0; i < str.length; i++) { | |
| cs = cs ^ str[i]; | |
| } | |
| cs &= 0xff; | |
| return cs; | |
| } | |
| function toUTF8Array(str) { | |
| //https://stackoverflow.com/questions/18729405/how-to-convert-utf8-string-to-byte-array | |
| var utf8 = []; | |
| for (var i = 0; i < str.length; i++) { | |
| var charcode = str.charCodeAt(i); | |
| if (charcode < 0x80) utf8.push(charcode); | |
| else if (charcode < 0x800) { | |
| utf8.push(0xc0 | (charcode >> 6), | |
| 0x80 | (charcode & 0x3f)); | |
| } | |
| else if (charcode < 0xd800 || charcode >= 0xe000) { | |
| utf8.push(0xe0 | (charcode >> 12), | |
| 0x80 | ((charcode >> 6) & 0x3f), | |
| 0x80 | (charcode & 0x3f)); | |
| } | |
| // surrogate pair | |
| else { | |
| i++; | |
| // UTF-16 encodes 0x10000-0x10FFFF by | |
| // subtracting 0x10000 and splitting the | |
| // 20 bits of 0x0-0xFFFFF into two halves | |
| charcode = 0x10000 + (((charcode & 0x3ff) << 10) | | |
| (str.charCodeAt(i) & 0x3ff)); | |
| utf8.push(0xf0 | (charcode >> 18), | |
| 0x80 | ((charcode >> 12) & 0x3f), | |
| 0x80 | ((charcode >> 6) & 0x3f), | |
| 0x80 | (charcode & 0x3f)); | |
| } | |
| } | |
| return utf8; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment