

Lets take a very simple example program to.

Maybe you can even drop the bitwise "and" here, but I can't test right now. The valueOf() method throws an exception named NumberFormatException if the string cannot be parsed as an integer. On earlier Java versions your best bet here might to use a long and then just put the lower 4 bytes of that long into an int: long x = Long.parseLong("C050005C", 16) As of Java 8 there's Integer.parseUnsignedInt (thanks, Andreas): int temp = Integer.parseIntUnsigned("C050005C",16)

It would fit into an unsigned integer, though. If it is one number & you want to remove separators, NumberFormat will return a number to you. This also removes the need to throw/catch the checked exception from other answers. The value represented by the string is not a value of type int. int i Integer.parseInt (myNumberString.replaceAll (',', '')) Take note that replaceAll is used, critical for any number >999,999.Any character of the string is not a digit of the specified radix, except that the first character may be a minus sign '-' ('\u002D') provided that the string is longer than length 1.The radix is either smaller than Character.MIN_RADIX or larger than Character.MAX_RADIX.The first argument is null or is a string of length zero. In Java there are two methods to convert String to Int, they are: Integer.parseInt () Integer.valueOf () Convert String to Int using Integer.parseInt () The Integer.parseInt () is a static method of Integer class used in Java to converts string to primitive integer.From the documentation of that method (emphasis mine):Īn exception of type NumberFormatException is thrown if any of the following situations occurs: This would cause an integer overflow, as integers are always signed in Java.
