1. Overview
On this tutorial, we’ll see how you can convert from int to char and again in Java.
2. char in Java
We’ll briefly talk about how characters are represented as a way to higher perceive the code we’ll see later within the article.
Internally, Java shops every char as a 16-bit Unicode encoded worth:
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 are able to simply verify this by casting the char worth to int:
assertEquals(65, (int)'A');
ASCII code is a subset of the Unicode encoding, representing principally the English alphabet.
3. Changing int to char
For example we’ve got an int variable with worth 7 and we need to convert it to its char counterpart ‘7‘. We’ve a number of choices to do that.
Merely casting it to char will not work as a result of this converts it to the character that is 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 are able to simply offset the ‘0‘ character by 7 to get the ‘7‘ character:
@Take a look at
public void givenAnInt_whenAdding0_thenExpectedCharType() {
int num = 7;
char c = (char)('0' + num);
assertEquals('7', c);
}
3.2. Utilizing the Character.forDigit() Methodology
Including ‘0‘ works, but it surely appears a bit hackish. Fortunately, we’ve got a a lot cleaner solution to do it utilizing the Character.forDigit() methodology:
@Take a look at
public void givenAnInt_whenUsingForDigit_thenExpectedCharType() {
int num = 7;
char c = Character.forDigit(num , 10);
assertEquals('7', c);
}
We are able to discover that the forDigit() methodology accepts a second argument, radix, which represents the bottom illustration of the quantity we need to convert. In our case, it is 10.
3.3. Utilizing the Integer.toString() Methodology
We are able to use the wrapper class Integer, which has the toString() methodology that converts the given int to its String illustration. After all, this can be utilized to transform a quantity with a number of digits to String. But, we are able to additionally use it to transform a single digit to char by chaining the charAt() methodology and selecting the primary char:
@Take a look at
public void givenAnInt_whenUsingToString_thenExpectedCharType() {
int num = 7;
char c = Integer.toString(num).charAt(0);
assertEquals('7', c);
}
4. Changing char to int
Beforehand, we noticed how you can convert an int to char. Let’s have a look at how we are able to get the int worth of a char. As we in all probability anticipate, casting the char to int will not work, as this provides us the decimal illustration of the UTF-16 encoding of the character:
@Take a look at
public void givenAChar_whenCastingFromCharToInt_thenExpectedUnicodeRepresentation() {
char c = '7';
assertEquals(55, (int) c);
}
4.1. Subtracting ‘0′
If after we add ‘0′ we get the char, then conversely by subtracting the ‘0′ decimal worth, we should always get the int:
@Take a look at
public void givenAChar_whenSubtracting0_thenExpectedNumericType() {
char c = '7';
int n = c - '0';
assertEquals(7, n);
}
This certainly works, however there are higher and easier methods to do it.
4.2. Utilizing the Character.getNumericValue() Methodology
The Character class once more offers one other helper methodology, getNumericValue(), which principally does what it says:
@Take a look at
public void givenAChar_whenUsingGetNumericValue_thenExpectedNumericType() {
char c = '7';
int n = Character.getNumericValue(c);
assertEquals(7, n);
}
4.3. Utilizing Integer.parseInt()
We are able to use Integer.parseInt() to transform a String to a numeric illustration. Simply as earlier than, whereas we are able to use this to transform a whole quantity with a number of digits to the int illustration, we are able to additionally use it for a single digit:
@Take a look at
public void givenAChar_whenUsingParseInt_thenExpectedNumericType() {
char c = '7';
int n = Integer.parseInt(String.valueOf(c));
assertEquals(7, n);
}
Certainly, the syntax is a bit cumbersome, principally as a result of it includes a number of conversions, but it surely works as anticipated.
5. Conclusion
On this article, we discovered how characters are internally represented in Java and the way we are able to convert an int to char and again.
As at all times, the whole code samples for this text might be discovered over on GitHub.