BigInteger Class

R

Razii

Is there any way to write all the numbers in BigInteger object without
calling toString()? I was testing a very large number and most of the
time is wasted in toString.
 
A

Arne Vajhøj

Razii said:
Is there any way to write all the numbers in BigInteger object without
calling toString()? I was testing a very large number and most of the
time is wasted in toString.

No - you can not write a BigInteger as String without converting it
to String.

And I doubt more efficient algorithms exist than the one that
comes with Java.

Arne
 
E

Eric Sosman

Razii said:
Is there any way to write all the numbers in BigInteger object without
calling toString()? I was testing a very large number and most of the
time is wasted in toString.

I'm not sure just what you want to do, but if you like
you can use toByteArray() to get the "raw" two's-complement
representation of the value.
 
R

Razii

if you like
you can use toByteArray() to get the "raw" two's-complement
representation of the value.

Yes, that's what I want. Byte Array with values in it (instead of
two's-complement representation). That would be much faster to write
to file. Especially if the BigInteger object has 1 million digits,
converting to String would take a very long time.
 
R

Razii

If you just want to write it out so you ran read it back in again
without looking at it, you can use serialisation.

Good suggestion but with serialisation you can't open the text file in
notepad and look at it in text file.

But it was faster ..

import java.io.*;
import java.math.*;
import java.util.*;

public class BigNumber {

public static void main(String[] args) throws Exception
{
BigInteger bigInteger = new BigInteger (10000000, new
Random());
ObjectOutputStream outStr = new ObjectOutputStream
(new FileOutputStream("number.txt"));
outStr.writeObject(bigInteger);
outStr.flush();
outStr.close();
}
}

This same thing didn't work with bufferedstream due very slow toString

The question still is how can I write the number (instead of object)
to file without toString...
 
R

Roedy Green

The question still is how can I write the number (instead of object)
to file without toString...

The problem is the internal form and external human-readable form are
quite different and it takes quite a bit of computation to
interconvert them. Your only hope is to find some representation that
takes less effort to export, e.g. hex.

Another approach is to get a HEX editor and look at a binary dump of
the file recorded in some internal binary format the way we used to in
the olden days.
 
R

Razii

The problem is the internal form and external human-readable form are
quite different and it takes quite a bit of computation to
interconvert them. Your only hope is to find some representation that
takes less effort to export, e.g. hex.

Another approach is to get a HEX editor and look at a binary dump of
the file recorded in some internal binary format the way we used to in
the olden days.

Ok... have a look at this version. It doesn't work exactly (especially
if you increase the length of the BigInteger. I am not sure where
exactly the problem is. If you can fix it, post the version

import java.io.*;
import java.math.*;
import java.util.*;

public class BigNumber {

public static void main(String[] args) throws Exception
{
FileWriter fstream = new FileWriter("number.txt");
BufferedWriter fout = new BufferedWriter(fstream);
PrintWriter out = new PrintWriter (fout);

BigInteger bigInteger = new BigInteger (30,new Random());

System.out.println(bigInteger);

byte[] byteArray = bigInteger.toByteArray();
int[] intArray = byteArrayToIntArray(byteArray, byteArray[0] <
0 ? -1 : 0);

for (int i : intArray)
{
System.out.println(i);
out.print(i);
}
out.flush();
}

/** Convert a big-endian byte array to a little-endian array of words.
*/
private static int[] byteArrayToIntArray(byte[] bytes, int sign)
{
// Determine number of words needed.
int[] words = new int[bytes.length/4 + 1];
int nwords = words.length;

// Create a int out of modulo 4 high order bytes.
int bptr = 0;
int word = sign;
for (int i = bytes.length % 4; i > 0; --i, bptr++)
word = (word << 8) | (bytes[bptr] & 0xff);
words[--nwords] = word;

// Elements remaining in byte[] are a multiple of 4.
while (nwords > 0)
words[--nwords] = bytes[bptr++] << 24 |
(bytes[bptr++] & 0xff) << 16 |
(bytes[bptr++] & 0xff) << 8 |
(bytes[bptr++] & 0xff);
return words;
}


}
 
E

Eric Sosman

Razii said:
Good suggestion but with serialisation you can't open the text file in
notepad and look at it in text file.

If the value has "1 million digits" (your words), looking
at it with a text editor will not be very informative.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top