This article explores different ways to convert a char to its ASCII code in Kotlin.

A simple solution to convert a char to its ASCII code is using the toInt() function, which returns the value of the character as an Int. Here’s a Kotlin program demonstrating its usage:

Download Code

 
Alternatively, we can get the value of this character as a Byte and convert this Byte value to Int:

Download Code

 
To convert each character of a string to its ASCII code, we can encode the string into a sequence of bytes using the getBytes() function, resulting in a new byte array. The following code example shows invocation for this method:

Download Code

That’s all about converting char to its ASCII code in Kotlin.