0

I am trying to convert a BigInteger or a String variable containing no.s into a BigInteger Array. How can i do this. please reply. for eg.

BigInteger = 278490638904984
BigInteger Array = [2,7,8,4,9,0,6,3,8,9,0,4,9,8,4]

4 Answers 4

3
public static void main(String[] args) {
    String s = "278490638904984";
    BigInteger[] big = new BigInteger[s.length()];
    for (int i = 0; i < s.length(); i++) {
        big[i] = new BigInteger(String.valueOf(s.charAt(i)));
    }
    System.out.println(Arrays.toString(big));
}

Output:

[2, 7, 8, 4, 9, 0, 6, 3, 8, 9, 0, 4, 9, 8, 4]
Sign up to request clarification or add additional context in comments.

Comments

1

You should be able to do this:

BigInteger bi = new BigInteger(myString);

You then will have to assign that to a position in an array of BigIntegers.

Note, if your string cannot be converted, it will throw a NumberFormatException.

Comments

1

For which purpose? It's a little bit strange...

BigInteger   bi = new BigInteger( "278490638904984" );
String       s  = bi.toString();
int          len = s.length();
BigInteger[] digits = new BigInteger[len];
for( int i = 0; i < len; ++i ) {
   digits[i] = BigInteger.valueOf( s.charAt(i)-'0' );
}

1 Comment

charAt(i) - '0' is equal to Character.toNumericValue()
0

Try to convert BigInteger value toString(), then get length of the String like str.length() and create an array of desired size.

After this you can fill the array with values:

array[i] = new BigInteger(str.substring(i, i+1));

It's not brilliant solution, but should do the job.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.