Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2008

Tip: Looking for answers? Try searching our database.

BigInteger Class

Thread view: 
Razii - 21 Mar 2008 02:03 GMT
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.
Arne Vajhøj - 21 Mar 2008 02:18 GMT
> 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
Eric Sosman - 21 Mar 2008 02:46 GMT
> 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.

Signature

Eric Sosman
esosman@ieee-dot-org.invalid

Razii - 21 Mar 2008 03:30 GMT
> I'm not sure just what you want to do,

Write each digit to a file instead of first converting it toString by

out.println(bigInteger);
Razii - 21 Mar 2008 06:31 GMT
>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.
Roedy Green - 21 Mar 2008 06:40 GMT
>Yes, that's what I want. Byte Array with values in it (instead of
>two's-complement representation).

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

See http://mindprod.com/jgloss/serialization.html
Signature


Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Razii - 21 Mar 2008 08:02 GMT
>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...
Roedy Green - 21 Mar 2008 08:29 GMT
On Fri, 21 Mar 2008 02:02:52 -0500, Razii
<DONTwhatevere3e@hotmail.com> wrote, quoted or indirectly quoted
someone who said :

>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.

Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Razii - 21 Mar 2008 11:12 GMT
>The problem is the internal form and external human-readable form are
>quite different and it takes quite a bit of computation to
[quoted text clipped - 4 lines]
>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;
  }

            
}
Eric Sosman - 21 Mar 2008 13:50 GMT
>> 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.

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

Signature

Eric Sosman
esosman@ieee-dot-org.invalid



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.