Wednesday, February 21, 2024
HomeSoftware EngineeringThe best way to Get the ASCII Worth of Character in Kotlin

The best way to Get the ASCII Worth of Character in Kotlin


The problem

Get the ASCII worth of a personality.

The answer in Kotlin

Possibility 1:

val getAscii = Char::toInt

Possibility 2:

enjoyable getAscii(c: Char) = c.code

Possibility 3:

enjoyable getAscii(c: Char): Int {
    return c.code.toByte().toInt()
}

Check instances to validate our answer

import kotlin.take a look at.assertEquals
import org.junit.Check

class TestExample {
  @Check
  enjoyable `Fundamental exams`() {
    assertEquals(65, getAscii('A'))
    assertEquals(32, getAscii(' '))
    assertEquals(33, getAscii('!'))
  }
}
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments