Parsing a Hexadecimal String to an Integer Throws a Numberformatexception

Parsing a Hexadecimal String to an Integer throws a NumberFormatException?

Will this help?

Integer.parseInt("00ff00", 16)

16 means that you should interpret the string as 16-based (hexadecimal). By using 2 you can parse binary number, 8 stands for octal. 10 is default and parses decimal numbers.

In your case Integer.parseInt(primary.getFullHex(), 16) won't work due to 0x prefix prepended by getFullHex() - get rid of and you'll be fine.

NumberFormatException - with String of an Hex Value

I've resolved my problem with this question

byte k = stream[i];

int b = 0;
try {
String hexStringNumber = String.format("0x%02X",k);
b = Integer.decode(hexStringNumber);
} catch (NumberFormatException e) {
e.printStackTrace();
}

hexString.toInt(32) NumberFormatException

First off, you seem to have misunderstood the radix, which is the second argument in valueOf and parseInt, and the only argument with the extension function toInt. It doesn't represent the bits in the target number type, it tells the method how to convert your string by informing it of what base it's operating in. Note that there is a second problem, which I will get back to later.

Radix is the base number of the underlying number system. By default it's 10 (the 0-arg toInt() method. toInt() calls parseInt, which looks like this:

public static int parseInt(String s) throws NumberFormatException {
return parseInt(s,10);
}

As you see, it uses radix = 10. Again, the bits of the number type isn't relevant here. Wikipedia covers it pretty well, and Java follows more or less the same system. As you see, 16 corresponds to hex. So the appropriate radix to use here is 16.

However, like I mentioned, there's a second problem. It will still crash, because the value can't be parsed. The resulting number is 3340525056, which is more than the max int value, which is 2147483647.

Which means, you can't use any of the int methods; you'll need to use Long ones.

So, for your case, this is fine:

val x = "C71C5E00".toLong(16)

Again, the radix is the number system to use, not the amount of bits in the resulting number. If it was, you'd need to use 64 for longs.

Bonus: ints and longs have a pre-determined amount of bits. By using toInt(), you're already implicitly requesting 32 bits. with a Long, you're requesting 64 bits.

Unexpected NumberFormatException while parsing a hex string to an int value

Your number represents a number greater than that assignable to an int. Try:

Long.parseLong("FFFF4C6A", 16);

which gives 4294921322.

From the doc:

An exception of type NumberFormatException is thrown if any of the following situations occurs:

  • The first argument is null or is a string of length zero.
  • The radix is either smaller than Character.MIN_RADIX or larger than Character.MAX_RADIX.
  • Any character of the string is not a digit of the specified radix, …
  • The value represented by the string is not a value of type int.

and it's the 4th case that you're hitting.

Java fails to parse a hex string as an int

As you are using Java 8, consider the Integer.parseUnsignedInt method:

Integer.parseUnsignedInt(hex, 16);  

why Java throws a NumberFormatException

That's because the default parse method expects a number in decimal format, to parse hexadecimal number, use this parse:

Byte.parseByte(st[i], 16);

Where 16 is the base for the parsing.

As for your comment, you are right. The maximum value of Byte is 0x7F. So you can parse it as int and perform binary AND operation with 0xff to get the LSB, which is your byte:

bytes[i] = Integer.parseInt(st[i], 16) & 0xFF;

How do I parse a hex-int from a string to an Integer?

There is actually a separate function where you can define the radix:

https://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#parseInt(java.lang.String,%20int)

Integer.parseInt(x) simply calls Integer.parseInt(x, 10), so you have to specify a radix 10 number.

In order to parse a hex string, you would simply have to use the following (note that the 0x prefix is not allowed):

Integer.parseInt("FFFFFFF", 16);

parsing Hexadecimal in Java

taking in account that FFFFFFFFFFFFFFB2 in HEX is -78 in decimal"

Nope, FFFFFFFFFFFFFFB2 hex is 18446744073709552000 decimal. You're mistaking hex for 2s complement.

If you want to take a 2s complement bit pattern and convert it into a number, this answer suggests a trick:

long l = new BigInteger("FFFFFFFFFFFFFFB2",16).longValue();

And it actually works.

Note I used BigInteger and long, rather than long and int, as the number is too big.

convert string to hex throws java.lang.NumberFormatException

If you want to just convert FFFFFF to 0xFFFFFF, then there is no need for .toInt() and the rest your code should work just fine.

If you want to convert the value of the color from hex to decimal, then you can use .toInt(16) function without the radix (0x) at the beginning, like this:

val color = picture.color.drop(1).toInt(16)

Or using Integer.decode(), like this:

val color = Integer.decode("0x${picture.color.drop(1)}")

NumberFormatException is throw on parsing a integer

The number you are passing is outside the range of integer which is from -2,147,483,648 to 2,147,483,647



Related Topics



Leave a reply



Submit