Skip to content

Instantly share code, notes, and snippets.

@drguildo
Created September 20, 2014 12:28
Show Gist options
  • Save drguildo/8c429c8557b373826dd5 to your computer and use it in GitHub Desktop.
Save drguildo/8c429c8557b373826dd5 to your computer and use it in GitHub Desktop.
Fletcher's checksum in Kotlin
package com.drguildo.stdlib.algorithms
fun fletcher16(data: ByteArray): Int {
var sum1 = 0
var sum2 = 0
for (d in data) {
sum1 = (sum1 + d) % 255
sum2 = (sum2 + sum1) % 255
}
return sum2.shl(8).or(sum1)
}
fun main(args: Array<String>) {
var n = readLine()!!.toInt()
for (i in 1..n)
println(java.lang.String.format("%d %X", i, fletcher16(readLine()!!.toByteArray())))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment