Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

In this tutorial, we’ll see how to convert from int to char and back in Java.

2. char in Java

We’ll briefly discuss how characters are represented in order to better understand the code we’ll see later in the article.

Internally, Java stores each char as a 16-bit Unicode encoded value:

Character 2 Bytes Decimal (base 10) Hex (base 16)
A 00000000 01000001 65 41
a 00000000 01100001 61 97
1 00000000 00110001 49 31
Z 00000000 01011010 90 5A

We can easily check this by casting the char value to int:

assertEquals(65, (int)'A');

ASCII code is a subset of the Unicode encoding, representing mostly the English alphabet.

3. Converting int to char

Let’s say we have an int variable with value 7 and we want to convert it to its char counterpart ‘7‘. We have a few options to do this.

Simply casting it to char won’t work because this converts it to the character that’s represented in binary as 0111, which in UTF-16 is U+0007 or the character ‘BELL’.

3.1. Offsetting by ‘0’

The characters in UTF-16 are represented in sequential order. So we can just offset the ‘0‘ character by 7 to get the ‘7‘ character:

@Test
public void givenAnInt_whenAdding0_thenExpectedCharType() {
    int num = 7;

    char c = (char)('0' + num);

    assertEquals('7', c);
}

3.2. Using the Character.forDigit() Method

Adding ‘0‘ works, but it seems a bit hackish. Luckily, we have a much cleaner way to do it using the  Character.forDigit() method:

@Test
public void givenAnInt_whenUsingForDigit_thenExpectedCharType() {
    int num = 7;

    char c = Character.forDigit(num , 10);

    assertEquals('7', c);
}

We can notice that the forDigit() method accepts a second argument, radix, which represents the base representation of the number we want to convert. In our case, it’s 10.

3.3. Using the Integer.toString() Method

We can use the wrapper class Integer, which has the toString() method that converts the given int to its String representation. Of course, this can be used to convert a number with multiple digits to String. But, we can also use it to convert a single digit to char by chaining the charAt() method and picking the first char:

@Test
public void givenAnInt_whenUsingToString_thenExpectedCharType() {
    int num = 7;

    char c = Integer.toString(num).charAt(0);

    assertEquals('7', c);
}

4. Converting char to int

Previously, we saw how to convert an int to char. Let’s see how we can get the int value of a char. As we probably expect, casting the char to int won’t work, as this gives us the decimal representation of the UTF-16 encoding of the character:

@Test
public void givenAChar_whenCastingFromCharToInt_thenExpectedUnicodeRepresentation() {

    char c = '7';

    assertEquals(55, (int) c); 
}

4.1. Subtracting ‘0’

If when we add ‘0’ we get the char, then conversely by subtracting the ‘0’ decimal value, we should get the int:

@Test
public void givenAChar_whenSubtracting0_thenExpectedNumericType() {

    char c = '7';

    int n = c - '0';

    assertEquals(7, n);
}

This indeed works, but there are better and simpler ways to do it.

4.2. Using the Character.getNumericValue() Method

The Character class again provides another helper method, getNumericValue(), which basically does what it says:

@Test
public void givenAChar_whenUsingGetNumericValue_thenExpectedNumericType() {

    char c = '7';

    int n = Character.getNumericValue(c);

    assertEquals(7, n);
}

4.3. Using Integer.parseInt()

We can use Integer.parseInt() to convert a String to a numeric representation. Just as before, while we can use this to convert an entire number with multiple digits to the int representation, we can also use it for a single digit:

@Test
public void givenAChar_whenUsingParseInt_thenExpectedNumericType() {

    char c = '7';

    int n = Integer.parseInt(String.valueOf(c));

    assertEquals(7, n);
}

Indeed, the syntax is a bit cumbersome, mostly because it involves multiple conversions, but it works as expected.

5. Conclusion

In this article, we learned how characters are internally represented in Java and how we can convert an int to char and back.

As always, the complete code samples for this article can be found over on GitHub.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.