Groovy web console

subscribe to the feed Subscribe
to this
site
GE Dinmap Checksummer (via #groovywebconsole)
tweet this snippet Tweet
this
script

GE Dinmap Checksummer

Published 9 months ago by BH, FH
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
String command = "PS"
byte[] commandBytes = command.bytes
byte spaceChar = ((byte) ' ')
byte[] ret = new byte[commandBytes.length + 3]
ret[0] = spaceChar
for (int i = 0; i < commandBytes.length; i++) {
  ret[i + 1] = commandBytes[i]
}
int sum = 0        
for (byte b in commandBytes) {
  sum += (b - spaceChar + 1)
  if (sum > 0xFFF) {
    sum -= 0xFFF
  }
}
ret[ret.length - 2] = (spaceChar + (sum >> 6))
ret[ret.length - 1] = ((spaceChar + (sum & 0x3F)) + 1)
println "**** command with checksum: ${new String(ret)}"