Skip to content

Instantly share code, notes, and snippets.

@lotharschulz
Last active August 5, 2018 17:15
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 lotharschulz/2f46e95f9460771cb5ec06e75c2c3041 to your computer and use it in GitHub Desktop.
Save lotharschulz/2f46e95f9460771cb5ec06e75c2c3041 to your computer and use it in GitHub Desktop.
Kotlin: sample code that converts individual characters of a string to Integers
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