The challenge

Get the ASCII value of a character.

The solution in Kotlin

Option 1:

1
val getAscii = Char::toInt

Option 2:

1
fun getAscii(c: Char) = c.code

Option 3:

1
2
3
fun getAscii(c: Char): Int {
    return c.code.toByte().toInt()
}

Test cases to validate our solution

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
import kotlin.test.assertEquals
import org.junit.Test

class TestExample {
  @Test
  fun `Basic tests`() {
    assertEquals(65, getAscii('A'))
    assertEquals(32, getAscii(' '))
    assertEquals(33, getAscii('!'))
  }
}