Converting Characters to Integers in Java

How can I convert a char to int in Java?

The ASCII table is arranged so that the value of the character '9' is nine greater than the value of '0'; the value of the character '8' is eight greater than the value of '0'; and so on.

So you can get the int value of a decimal digit char by subtracting '0'.

char x = '9';
int y = x - '0'; // gives the int value 9

Fastest way to convert numeric chars to int

  1. The two versions are not equivalent:

    • The Character.getNumericalValue(...) methods work for a variety of characters that represent digits or numbers, and it will return -1 or -2 in cases where the character doesn't represent a non-negative integer.
    • The num - '0' approach only gives the correct answer for the codepoints that correspond to the ASCII characters '0' through '9'. For all other codepoints or codeunits, it gives a meaningless value.
  2. The num - '0' version will be faster. This is clear from looking at the source code for getNumericalValue(...).

  3. While the difference is significant in relative terms, it is very small in absolute terms.


I concur with the comments that say that this is most likely a premature optimization.

It is also an incorrect optimization in some contexts.



I use it a lot so was wondering if I was using the most efficient one.

This is definitely premature optimization :-)

The number of times you write a particular code sequence is unrelated to performance of the code sequence when is executed. A section of code is only worth optimizing if the time spent executing it makes a significant difference to your entire application.

How do I convert a String to an int in Java?

String myString = "1234";
int foo = Integer.parseInt(myString);

If you look at the Java documentation you'll notice the "catch" is that this function can throw a NumberFormatException, which you can handle:

int foo;
try {
foo = Integer.parseInt(myString);
}
catch (NumberFormatException e) {
foo = 0;
}

(This treatment defaults a malformed number to 0, but you can do something else if you like.)

Alternatively, you can use an Ints method from the Guava library, which in combination with Java 8's Optional, makes for a powerful and concise way to convert a string into an int:

import com.google.common.primitives.Ints;

int foo = Optional.ofNullable(myString)
.map(Ints::tryParse)
.orElse(0)

Java - char, int conversions

The first example (which compiles) is special because both operands of the addition are literals.

A few definitions to start with:

  • Converting an int to char is called a narrowing primitive conversion, because char is a smaller type than int.

  • 'A' + 1 is a constant expression. A constant expression is (basically) an expression whose result is always the same and can be determined at compile-time. In particular, 'A' + 1 is a constant expression because the operands of + are both literals.

A narrowing conversion is allowed during the assignments of byte, short and char, if the right-hand side of the assignment is a constant expression:

In addition, if the expression [on the right-hand side] is a constant expression of type byte, short, char, or int:

  • A narrowing primitive conversion may be used if the variable is of type byte, short, or char, and the value of the constant expression is representable in the type of the variable.

c + 1 is not a constant expression, because c is a non-final variable, so a compile-time error occurs for the assignment. From looking at the code, we can determine that the result is always the same, but the compiler isn't allowed to do that in this case.

One interesting thing we can do is this:

final char a = 'a';
char b = a + 1;

In that case a + 1 is a constant expression, because a is a final variable which is initialized with a constant expression.

The caveat "if […] the value […] is representable in the type of the variable" means that the following would not compile:

char c = 'A' + 99999;

The value of 'A' + 99999 (which is 100064, or 0x186E0) is too big to fit in to a char, because char is an unsigned 16-bit integer.


As for the postfix ++ operator:

The type of the postfix increment expression is the type of the variable.

...

Before the addition, binary numeric promotion* is performed on the value 1 and the value of the variable. If necessary, the sum is narrowed by a narrowing primitive conversion and/or subjected to boxing conversion to the type of the variable before it is stored.

(* Binary numeric promotion takes byte, short and char operands of operators such as + and converts them to int or some other larger type. Java doesn't do arithmetic on integral types smaller than int.)

In other words, the statement c++; is mostly equivalent to:

c = (char)(c + 1);

(The difference is that the result of the expression c++, if we assigned it to something, is the value of c before the increment.)

The other increments and decrements have very similar specifications.

Compound assignment operators such as += automatically perform narrowing conversion as well, so expressions such as c += 1 (or even c += 3.14) are also allowed.

Char to int and back again

There's a misunderstanding in how Character.getNumericValue(char) works.

Returns the int value that the specified Unicode character represents. For example, the character '\u216C' (the roman numeral fifty) will return an int with a value of 50.

The letters A-Z in their uppercase ('\u0041' through '\u005A'), lowercase ('\u0061' through '\u007A'), and full width variant ('\uFF21' through '\uFF3A' and '\uFF41' through '\uFF5A') forms have numeric values from 10 through 35. This is independent of the Unicode specification, which does not assign numeric values to these char values.

from the java docs.

In other words:

The method getNumericValue does not produce the UTF-16 value of the specified character, but attempts to produce a numeric value - in the sense that '0' will produce 0 - from the given value. And 'A' corresponds to 10 in hex.

But 10 is not the UTF16 value corresponding to 'A'.

A correct way of converting a char to it's corresponding UTF16-value would be to use one of the codepoint-methods from Character. Or if you're absolutely positive, all values will be of the BMP, you can use

char c = 'A';
int i = (int) c;

In this case Character.toChars(int) will also work.

How do I convert a char [] to an int?

char[] c = {'3', '5', '9', '3'};
int number = Integer.parseInt(new String(c));


Related Topics



Leave a reply



Submit