I have BigInt
BigInteger i = new BigInteger("5876934265987526278534978564378568734564937563487564327564376534875483753475");
I need to convert it to [] int
. How can I do this fast?
My method is very slow
private static int[] convertDigitsToIntArray(java.math.BigInteger x) {
String s = x.toString();
int[] result = new int[s.length()];
for (int i = 0; i < s.length(); i++) {
result[i] = Integer.parseInt(String.valueOf(s.charAt(i)));
}
return result;
}
Convert it to a string and then iterate the characters:
int[] getArr(BigInteger num)
{
String str = num.toString();
int[] arr = new int[str.length()];
for (int i=0; i<arr.length; i++)
arr[i] = str.charAt(i)-'0';
return arr;
}
May be this will help...
public static void main(String[] args) {
BigInteger i = new BigInteger("5876934265987526278534978564378568734564937563487564327564376534875483753475");
String iStr = i.toString();
int[] intArray = new int[iStr.length()];
for(int j=0; j<iStr.length(); j++) {
intArray[j] = Integer.parseInt(String.valueOf(iStr.charAt(j)));
}
}
Try using this:
private static int[] convertDigitsToIntArray(java.math.BigInteger x) {
String s = x.toString();
int[] result = new int[s.length()];
for (int i = 0; i < s.length(); i++) {
result[i] = s.charAt(i) - '0';
}
return result;
}
You can convert the BigInteger to a String, get the char array of that string and iterate over that:
java.math.BigInteger source = new java.math.BigInteger("5876934265987526278534978564378568734564937563487564327564376534875483753475");
char[] array = source.toString().toCharArray();
int[] result = new int[array.length];
for(int i=0; i<array.length; i++)
{
result[i] = array[i] - '0';
}
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.