How to convert from a String to an integer in Java

The Integer.parseInt() and Long.parseLong() methods can be used to convert a string to an integer in Java. At its simplest, this means that you can convert a string to an integer with the following line of Java:

int val = Integer.parseInt(string);

Using Integer.parseInt() will work for the simplest cases. But there are some issues to be aware of:

When to use Integer.parseInt() and when to use Long.parseLong()?

A Java int is a 4-byte signed number. This means that the range of an int goes from approximately minus to plus 2.1 billion (strictly speaking, from -232 to 231-1). You can query the actual minimum and maximum values of an int from Java with the following code:

System.out.println(Integer.MAX_VALUE);
System.out.println(Integer.MIN_VALUE);

This range is too small for many real-life numbers. For example, if the number represents a file size, then 2.1 billion bytes is only just over 2GB. (This is why some older file systems and file formats had a file size limit of 2 or 4GB.) Times are another common case where an int is not sufficient, since these are usually stored as a number of milliseconds or even nanoseconds, and can grow to large magnitudes.

In many everyday cases, the solution is to use a long to store the value. A Java long is an 8-byte (signed) value. (That means it's equivalent to a long long in some versions of C, for example.) A long will therefore store values in a range of around minus to plus 16 billion billion, and while by no means infinite, this range is sufficient for most practical purposes. If in doubt, long and Long.parseLong() are therefore a safer choice:

long fileSize = Long.parseLong(string);

Parsing or converting numbers in different bases (e.g. hexadecimal)

In some cases, the number that you want to convert to an int may be in another base such as hexadecimal. The Integer class can deal with these nubmers in a couple of ways. Firstly, common bases (decimal, hexadecimal, octal) are generally handled by the Integer.decode() method, provided that the string in questiomn sticks to one of the common formats (e.g. if it is a hex string, then it is prefixed with 0x or # to indeicate this). For example, we can convert the hex number 0x123456 to decimal as follows:

String hexNo = "0x123456";
int val = Integer.decode(hexNo);
System.out.println("Decimal: " + val);

Care must be taken when using Integer.decode(). It will only handle the above common formats for hexadecimal and, more crucially, if the string begins with 0, it will be assumed to be octal. (Octal is base 8, sometimes used to represent file permissions, but otherwise not in common usage.)

How about converting from other bases, or converting from a hex number that is not prefixed with a hash or 0x? In these cases, the Integer.parseInt() can be passed a base number or radix as its second parameter. So to convert a "bare" hex number to decimal, we can do the following:

String hexNo = "123456";
int val = Integer.parseInt(hexNo, 16);
System.out.println("Decimal: " + val);

Another important case is base 2, or binary. Therefore, we can convert a binary number to decimal as follows:

String binaryNo = "11011";
int val = Integer.parseInt(hexNo, 2);
System.out.println("Decimal: " + val);

Parsing or converting unsigned numbers

Java ints and longs are signed. This means that they use one bit of their 32 or 64 bits to store the sign, at the expense of having a smaller magnitude. For example, a signed int can store a range of between approx -2 billion and +2 billion. If that integer was treated as unsigned instead, then it could theoretically represent a range of 0 to 4 billion instead.

Although Java's types are signed, sometimes when reading data from other systems, we need to be able to parse integers as though they were unsigned. To parse unsigned numbers, Java provides the methods Integer.parseUnsignedInt() and Long.parseUnsignedLong. These methods can also take a base or radix, so that we can parse an unsigned hex string by passing 16 as the second argument. So we could parse a 64-bit hexadecimal hash code as follows:

long hashValue = Long.parseUnsignedLong(hashStr, 16);

Of course, once the number has been parsed, it will be stored as the equivalent signed value in Java. See the page on Java and unsigned for more information.


If you enjoy this Java programming article, please share with friends and colleagues. Follow the author on Twitter for the latest news and rants.

Editorial page content written by Neil Coffey. Copyright © Javamex UK 2021. All rights reserved.