1. Introduction

In this article, we’ll discuss converting between an Int and Char in Kotlin. This is a specific aspect of a common problem developers face, converting from one type to another. We already have everything we need for this purpose, so we won’t write any logic from scratch but will explore the solutions Kotlin provides us. We’ll look at code samples that demonstrate how to use different functions and properties for this purpose and how they differ from each other.

2. Convert Int to Char

Let’s start with converting an Int to a Char. We’ll learn two approaches to type conversion. The first one allows us to perform a regular Int to Unicode Character conversion, and the second allows us to perform a conversion using a base N calculation.

2.1. Regular Int to Unicode Character Conversion

In Kotlin, the Int class contains a toChar() member function that converts an Int in the Char.MIN_VALUE Char.MAX_VALUE (0-65535) range to a 16-bit Unicode Character:

fun `Given an Int, When it's a valid number, Then should convert to the corresponding Unicode character`() {
    assertEquals('A', 65.toChar())
    // The escaped Unicode representation of 'A'
    assertEquals('\u0041', 65.toChar())

This is a basic Int to Char conversion. Kotlin has another digitToChar() extension function for Int that performs a conversion using another approach. Let’s move to the next section and explore it.

2.2. Converting with digitToChar()

The digitToChar() function differs from the previously mentioned one. Here, we perform the conversion using a base N calculation. When used without an argument, it returns a Char that represents a decimal digit (base 10) and throws an exception if the value isn’t in the range of 0-9:

fun `Given an Int, When it's a valid digit, Then should convert to the corresponding Char`() {
    assertEquals('0', 0.digitToChar())
    assertEquals('9', 9.digitToChar())

    assertThrows<IllegalArgumentException> { (-1).digitToChar() }
    assertThrows<IllegalArgumentException> { 10.digitToChar() }

We can also pass a radix to this function to convert the given digit to the given base N (2-36) representation:

fun `Given an Int, When it's a valid digit, Then should convert to the corresponding Char with the specified radix`() {
    assertEquals('1', 1.digitToChar(2))
    assertThrows<IllegalArgumentException> { 2.digitToChar(2) }

    assertEquals('F', 15.digitToChar(16))
    assertThrows<IllegalArgumentException> { 16.digitToChar(16) }

    assertEquals('Z', 35.digitToChar(36))
    assertThrows<IllegalArgumentException> { 36.digitToChar(36) }

We discussed how to convert an Int to a Char. Now, let’s see how we can perform the opposite operation.

3. Convert Char to Int

In this example, we’ll explore converting a regular Unicode Character to Int and how to use a base N conversion.

3.1. Regular Unicode Character to Int Conversion

When it comes to converting a Char to an Int, Kotlin has а toInt() member function similar to toChar() but it has been deprecated since Kotlin 1.5. Note that the deprecation notice also suggests deprecating the toChar() function from the previous section.

Instead, we should use the Char.code property for this purpose:

fun `Given a Char, When it's a valid Unicode character, Then should convert to the corresponding Int`() {
    assertEquals('A'.code, 65)
    assertEquals('\u0041'.code, 65)

Now, let’s move to the next section and see how to perform the base N conversion.

3.2. Converting with digitToInt() and digitToIntOrNull()

The digitToInt() extension function performs the opposite operation of digitToChar() in the case of Char. This function throws an exception if the Char isn’t a valid decimal digit, but here we also have a digitToIntOrNull() function that returns null in such cases:

fun `Given a Char, When it's a valid digit, Then should convert to the corresponding Int`() {
    Assertions.assertEquals(0, '0'.digitToInt())
    Assertions.assertEquals(9, '9'.digitToInt())

    assertThrows<IllegalArgumentException> { 'A'.digitToInt() }

If we use this function with a radix, we’ll convert the base N (2-36) char digit into its decimal representation:

fun `Given a Char, When it's a valid digit, Then should convert to the corresponding Int with the specified radix`() {
    assertEquals(1, '1'.digitToInt(2))
    // Not a binary digit

    assertEquals(15, 'F'.digitToInt(16))
    // Not a Hexadecimal digit

    assertEquals(35, 'Z'.digitToInt(36))
    // Not a Hexatrigesimal digit

The digitToIntOrNull() function also has an overload accepting a radix.

4. Conclusion

In this article, we’ve learned how to convert between Int and Char. To sum up, there are two types of conversion behaviors:

  • Converting an Int to the corresponding 16-bit Unicode character and vice versa.
  • Converting a digit to a Char using a base N calculation and vice versa.

We saw the new preferred way of converting a Char to an Int with the Char.code property. Don’t forget that the toInt() function is deprecated and the other to…() functions will probably also be deprecated in the future.

As always, the full source code of the examples is available over on GitHub.

Comments are closed on this article!