Created
September 20, 2014 12:28
-
-
Save drguildo/8c429c8557b373826dd5 to your computer and use it in GitHub Desktop.
Fletcher's checksum in Kotlin
This file contains 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
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