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