2
votes

I need to convert hex->dec using groovy, i have

def hex = 0x15B546EF13361F7B530C59332C60C2CE
int i= Integer.parseInt(hex,16);

When i run this, the error is actually showing me the value i need, but i cant get it

groovy.lang.MissingMethodException: No signature of method: static java.lang.Integer.parseInt() is applicable for argument types: (java.math.BigInteger, java.lang.Integer) values: [28855032353026779507009821653742961358, ...] Possible solutions: parseInt(java.lang.String, int), parseInt(java.lang.String) error at line: 4

If i give the hex as string as the error suggests:

def hex = '0x15B546EF13361F7B530C59332C60C2CE'

java.lang.NumberFormatException: For input string: "0x15B546EF13361F7B530C59332C60C2CE"

What am i missing?

1

1 Answers

3
votes
def hex = 0x15B546EF13361F7B530C59332C60C2CE
int i = hex // int i = (hex as int)
println i

Groovy stores number by default in BigDecimals. 0x... notation is just different presentation of BigDecimal for compiler.

But as you can see: your hex number is very very big - so you get value that is wrong. int type is incapable of storing so big value: Integer.MAX_VALUE == 2147483647 (even long type: Long.MAX_VALUE == 9223372036854775807) You should be using standard BigDecimal for this or BigInteger.