Last active
August 5, 2018 17:15
-
-
Save lotharschulz/2f46e95f9460771cb5ec06e75c2c3041 to your computer and use it in GitHub Desktop.
Kotlin: sample code that converts individual characters of a string to Integers
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
fun main(args: Array<String>) { | |
val someText = "ABC1234567890" | |
for (i in (0 .. (someText.length-1))){ | |
println(" number[$i]: ${someText[i]}") | |
println("(number[$i] is Char): ${(someText[i] is Char)}") | |
/* toInt return ascii dev code of a Character, | |
_not_ the Character as Int in case of numbers */ | |
println(" number[$i].toInt(): ${someText[i].toInt()}") | |
println(" number[$i].toString().toIntOrNull(): " + | |
"${someText[i].toString().toIntOrNull()}") | |
/* ascii to Int : 48 is ascii code for '0' */ | |
println(" number[$i].toInt() - 48: ${someText[i].toInt() - 48}") | |
/* subtracts character zero ('0') thats ascii dec code is 48 */ | |
println(" number[$i] - '0': ${someText[i] - '0'}") | |
println("-----") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment