English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Kotlin Program to Find ASCII Value of a Character

Comprehensive Collection of Kotlin Examples

In this program, you will learn how to find and display the ASCII value of a character in Kotlin. This is done through type conversion and regular variable assignment operations.

Example: Find the ASCII value of a character

fun main(args: Array<String>) {
    val c = 'a'
    val ascii = c.toInt()
    println("The ASCII value of $c is: $ascii")
}

When running the program, the output is:

The ASCII value of 'a' is: 97

In the above program, the character a is stored in the char variable ch. Similar to Java, double quotes (" ") are used to declare strings, while single quotes (' ') are used to declare characters.

Now, to find the ASCII value of ch, we use Kotlin's built-in function toInt(). This converts a Char value to an Int value.

Then we store the converted value in the variable ascii.

Finally, we use the println() function to print the ascii value.

This is the equivalent code in Java: Find the ASCII value in JavaASCII Value of a Character

Comprehensive Collection of Kotlin Examples