Skip to content

Instantly share code, notes, and snippets.

@ssaurel
Last active July 6, 2018 08:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ssaurel/cb18c7dc4113e7e348614fbb2dadde1f to your computer and use it in GitHub Desktop.
Save ssaurel/cb18c7dc4113e7e348614fbb2dadde1f to your computer and use it in GitHub Desktop.
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