Last active
July 6, 2018 08:47
-
-
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
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.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