Could anyone help me in converting an 18 digit string numeric to BigInteger in java
ie;a string "0x9999999999999999"
should appear as 0x9999999999999999
numeric value.
You can specify the base in BigInteger constructor.
BigInteger bi = new BigInteger("9999999999999999", 16);
String s = bi.toString(16);
If the String always starts with "0x" and is hexadecimal:
String str = "0x9999999999999999";
BigInteger number = new BigInteger(str.substring(2));
better, check if it starts with "0x"
String str = "0x9999999999999999";
BigInteger number;
if (str.startsWith("0x")) {
number = new BigInteger(str.substring(2), 16);
} else {
// Error handling: throw NumberFormatException or something similar
// or try as decimal: number = new BigInteger(str);
}
To output it as hexadecimal or convert to an hexadecimal representation:
System.out.printf("0x%x%n", number);
// or
String hex = String.format("0x%x", number);
Do you expect the number to be in hex, as that is what 0x usually means?
To turn a plain string into a BigInteger
BigInteger bi = new BigInteger(string);
String text = bi.toString();
to turn a hexidecimal number as text into a BigInteger and back.
if(string.startsWith("0x")) {
BigInteger bi = new BigInteger(string.sustring(2),16);
String text = "0x" + bi.toString(16);
}
BigInteger bigInt = new BigInteger("9999999999999999", 16);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.