Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Binary Converter object for the Binary Converter Android App on the SSaurel's Channel
package com.ssaurel.binaryconverter
object BinaryConverter {
// ...
fun binaryToString(binary:String) : String {
if (!isBinary(binary))
return "Not a binary value";
val chars = CharArray(binary.length / 8)
var i = 0
while (i < binary.length) {
val str = binary.substring(i, i + 8)
val nb = Integer.parseInt(str, 2)
chars[i / 8] = nb.toChar()
i += 8
}
return String(chars)
}
fun isBinary(txt:String?):Boolean {
if (txt != null && txt.length % 8 == 0) {
for (c in txt.toCharArray()) {
if (c != '0' && c!= '1')
return false
}
return true
}
return false
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment