Java char array to int
Solution 1:
Does the char[]
contain the unicode characters making up the digits of the number? In that case simply create a String from the char[]
and use Integer.parseInt:
char[] digits = { '1', '2', '3' };
int number = Integer.parseInt(new String(digits));
Solution 2:
Even more performance and cleaner code (and no need to allocate a new String object):
int charArrayToInt(char []data,int start,int end) throws NumberFormatException
{
int result = 0;
for (int i = start; i < end; i++)
{
int digit = (int)data[i] - (int)'0';
if ((digit < 0) || (digit > 9)) throw new NumberFormatException();
result *= 10;
result += digit;
}
return result;
}