## hexString.toInt(32) NumberFormatException

I

have a 32-bit hexadecimal value and I want to convert it to an integer.

Given the hexadecimal string `C71C5E00`

, the following methods all provide the following error:

java.lang.NumberFormatException: For input string: “C71C5E00”

```
"C71C5E00".toInt(32)
Integer.valueOf("C71C5E00", 32)
```

Kotlin docs declares that an int *represents a 32-bit signed integer*, So it’s not that the value is too big to be packed into an Int. I tried adding `0x`

before the string but it didn’t work.

**EDIT**: According to this issue I tried:

```
java.lang.Integer.parseInt("C71C5E00", 32)
```

Unfortunately, I still get the same error.

I don’t touch Android or Kotlin very often, please forgive my ignorance.

### Solution

First, you seem to have misunderstood cardinality, which is the second argument in `valueOf`

and `parseInt`

, and `the only argument`

to the extension function toInt. It does not represent bits in the target numeric type, it tells the method how to convert your string by telling it what base is. It’s running. Note that there is a second issue, which I will come to later.

The cardinality is the cardinality of the underlying numeric system. By default, it is the 10(0-arg `toInt()`

method. `toInt()`

calls `parseInt`

as follows:

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

As you can see, it uses radix = 10. Again, bits of numeric type are not relevant here. Wikipedia covers it well, and Java follows more or less the same system. As you can see, 16 corresponds to hexadecimal. So the appropriate cardinality used here is 16.

**But**, as I mentioned, there is a second problem. It still crashes because the value cannot be resolved. The resulting number is `3340525056`

, which is greater than the max int value of 2147483647.

This means that you cannot use any int methods; You need to use `Long`

.

So for your case, it’s fine:

```
val x = "C71C5E00".toLong(16)
```

Again, cardinality is the number system to use, not the number of digits in the resulting number. If it is, 64 needs to be used to represent bulls.

Benefits: Integers and long integers have predetermined number of digits. By using `toInt(),`

you are already implicitly requesting 32 bits. For Long, you are requesting 64-bit.