2

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;
}
Finkelson
  • 2,921
  • 4
  • 31
  • 49
  • 1
    You must use `Notepad++` or `EditPadPro` and use a macro.. You don't need programming here I think.. you need regular record macro and run it. – SSpoke May 10 '14 at 15:55
  • Call `toString()` and then convert each character to an `int`? The `toString` is likely to be fairly well optimised. – Alan Stokes May 10 '14 at 15:55
  • @Alan Stokes It's too slow approach. – Finkelson May 10 '14 at 15:58
  • what would your result int[] be if the bigint is `new BigInteger("-123")` – Kent May 10 '14 at 15:58
  • I don't have negative values. – Finkelson May 10 '14 at 15:59
  • To convert a char to a digit use `Character.digit(s.charAt(i), 10)` - that will be much faster than what you have. – Alan Stokes May 10 '14 at 16:10
  • @user It's never going to get a lot faster. Converting to decimal is inherently complex (lots of division). The final step of converting characters to digits is trivial by comparison. – Alan Stokes May 10 '14 at 16:13
  • It would be polite to credit @Nielarshi when you paste his answer in your question. – Alan Stokes May 10 '14 at 16:16
  • If you really need fast conversion to decimal, maybe you should do your computations in decimal instead of binary - see e.g. http://stackoverflow.com/a/1694615/212870. But I don't know of an existing implementation of that in Java. – Alan Stokes May 10 '14 at 16:27

5 Answers5

4

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;
}
barak manos
  • 29,648
  • 10
  • 62
  • 114
1

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)));
      }
  }
Nielarshi
  • 1,126
  • 7
  • 11
0

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;
}
Valdrinium
  • 1,398
  • 1
  • 13
  • 28
0

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';
}
Stefan Winkler
  • 3,871
  • 1
  • 18
  • 35
0
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++) {
        // Change your code to subtract the char '0'
        result[i] = s.charAt(i) - '0';
    }
    return result;
}   
user1416932
  • 257
  • 3
  • 6