Java Number Conversion

Learn the intricacies of Java Number to String Conversion and vice-versa.

β€œIt is a good thing for an uneducated man to read books of quotations.”
― Winston S. Churchill

1. Introduction

Number conversion is a frequent need while programming. You may have a need to convert from a hex string, decimal string or octal string. Or maybe you are using a number from a different base. The reverse conversion is also required many times: converting from a decimal integer to hex string, octal string, etc. Let us see how we can do these conversions.

2. Using Integer.decode()

The Integer class provides a static function decode() which can convert a decimal, hex, or octal string to an Integer.

Decimal Conversion

String value = "356";
System.out.println(value + " => " + Integer.decode(value));
# prints
356 => 356

Hex Conversion with the prefix 0x and 0X.

String value = "0xf6";
System.out.println(value + " => " + Integer.decode(value));
# prints
0xf6 => 246
String value = "0XFA";
System.out.println(value + " => " + Integer.decode(value));
# prints
0XFA => 250

Hex Conversion using the prefix #. (Similar to CSS color codes)

String value = "#ef";
System.out.println(value + " => " + Integer.decode(value));
# prints
#ef => 239

Octal Conversion

String value = "0765";
System.out.println(value + " => " + Integer.decode(value));
# prints
0765 => 501

3. Using Integer.parseInt()

The method Integer.decode() returns an Integer object. If you rather prefer a primitive int directly, you can use Integer.parseInt().

For base 10 conversion, you need not specify the radix.

String value = "985";
System.out.println(value + " => " + Integer.parseInt(value));

For other than base 10 conversion, specify the radix.

String value = "dead";
System.out.println(value + " => " + Integer.parseInt(value, 16));
# prints
dead => 57005

Prefixes such as “0x”, “0X”, “#” and “0” are not recognized and cause an error.

String value = "0xfe";
System.out.println(value + " => " + Integer.parseInt(value, 16));
# prints
Exception in thread "main" java.lang.NumberFormatException: For input string: "0xfe"

So do characters that are invalid for the specified base.

String value = "986";
System.out.println(value + " => " + Integer.parseInt(value, 8));
# prints
Exception in thread "main" java.lang.NumberFormatException: For input string: "986"

4. Converting Number to String: Using Integer.to*String()

When you need to convert from a number to a String, the Integer class provides several methods. Convert to radix 10 using toString().

int value = 0xfeaf;
System.out.println(value + " => " + Integer.toString(value));
# prints
65199 => 65199

And of course, we also have the corresponding counterparts: toBinaryString(), toHexString() and toOctalString().

System.out.println(value + " => " + Integer.toBinaryString(value));
System.out.println(value + " => " + Integer.toHexString(value));
System.out.println(value + " => " + Integer.toOctalString(value));
# prints
65199 => 1111111010101111
65199 => feaf
65199 => 177257

To obtain the value in a specific radix, use the two-argument form of toString().

System.out.println(value + " => " + Integer.toString(value, 36));
# prints
65199 => 1eb3

5. Conversion to Other Numeric Types

Sometimes you may need to convert an integer to a double, or a short to a float. How can you do that? Well, each of the numeric classes (Integer, Short, etc) extends from the Number class which support conversion to any of: byte, int, short, long, float and double. Here are some examples:

String value = "975";
Integer ival = Integer.valueOf(value);
System.out.println(value + " (byte) " + ival.byteValue());
System.out.println(value + " (int ) " + ival.intValue());
System.out.println(value + " (shrt) " + ival.shortValue());
System.out.println(value + " (long) " + ival.longValue());
System.out.println(value + " (flt ) " + ival.floatValue());
System.out.println(value + " (dbl ) " + ival.doubleValue());
# prints
975 (byte) -49
975 (int ) 975
975 (shrt) 975
975 (long) 975
975 (flt ) 975.0
975 (dbl ) 975.0

Notice: no warnings or errors about truncation. So use this conversion with care! Here is an example converting a double.

String value = "3.1415926";
Double dval = Double.valueOf(value);
System.out.println(value + " (byte) " + dval.byteValue());
System.out.println(value + " (int ) " + dval.intValue());
System.out.println(value + " (shrt) " + dval.shortValue());
System.out.println(value + " (long) " + dval.longValue());
System.out.println(value + " (flt ) " + dval.floatValue());
System.out.println(value + " (dbl ) " + dval.doubleValue());
# prints
3.1415926 (byte) 3
3.1415926 (int ) 3
3.1415926 (shrt) 3
3.1415926 (long) 3
3.1415926 (flt ) 3.1415925
3.1415926 (dbl ) 3.1415926

Review

That completes the lesson on the basics of converting string to number
and vice-versa. Integer.decode() can convert a string with a
prefix such as “0X”, “0” or “#”, whereas Integer.parseInt()
requires the radix to be specified.