Reputation: 3615
I am trying to convert a SQL function into Groovy for usage in Elasticsearch, but I am stuck at this stage. Considering, I have never touched java or groovy in my life, what am I doing wrong?
CODE
public String convertFromBaseToBase(String str, int fromBase, int toBase) {
return Integer.toString(Integer.parseInt(str, fromBase), toBase);
}
output = convertFromBaseToBase("8f8f87878f8f8080", 16, 10);
System.out.print(output);
Taken from Convert from one base to another in Java
ERROR
java.lang.NumberFormatException: For input string: "8f8f87878f8f8080"
at java_lang_Integer$parseInt.call(Unknown Source)
at Script1.convertFromBaseToBase(Script1.groovy:2)
at Script1.run(Script1.groovy:5)
Upvotes: 1
Views: 102
Reputation: 44740
8f8f87878f8f8080
is a very big number (10344635885392199808
in decimal), and will not fit inside an Integer
.
You will have to use BigInteger
for such big numbers.
public static String convertFromBaseToBase (String str, int fromBase, int toBase){
return (new BigInteger(str, fromBase)).toString(toBase);
}
Working Code -->
https://www.onlinegdb.com/B1mMEcKPX
BigInteger -->
https://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html#toString(int)
Upvotes: 2