This article explores different ways to convert between char and int in Kotlin.

1. Char to Int

A simple solution to get the value of a character as an Int is with the toInt() function.

Download Code

 
To convert a complete string to a sequence of bytes, use the getBytes() function, which returns a byte array.

Download Code

 
To get the numeric value represented by the character in the specified radix, use the Character.digit() function.

Download Code

2. Int to Char

To convert an Int value to a Char, use the toChar() function. The Int value should be in the range of Char codes Char.MIN_VALUE..Char.MAX_VALUE.

Download Code

 
To get the corresponding character representation of the specified “digit” in the specified radix, use the Character.forDigit() function.

Download Code

 
To convert the specified character (Unicode code point) to its UTF-16 representation, use the Character.toChars() function, which returns a char array.

Download Code

That’s all about converting between char and int in Kotlin.