Converting an integer into a BigInteger

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• 09-20-2010, 06:38 AM
mzjazzygirl
Converting an integer into a BigInteger
Hello All,

I am working an assignment, and I am confused on how to convert an integer into a BigInteger. What I want to do is use the divideAndRemainder(BigInteger val) function, but to use this function I need to pass a BigInteger.

I want to convert the integer 128 to a BigInteger.

Below is my code:
Code:

```import java.io.*; import java.math.*; import java.util.*; public class Homework1 { static BigInteger[] great; static BigInteger[] key; static char[] plaintext = new char[1000]; static int count; static int count1; static int count2; static long base = 128; static BigInteger base2 = BigInteger.valueOf(base); <--- This is where I am trying to do the conversion, but it is not correct public static void readBigNumbers() throws IOException { FileReader fr = new FileReader("encryption.txt");   BufferedReader br = new BufferedReader(fr);     String line;   System.out.println("New base from 128, to big integer... "+ base2);   count = 0;   while ((line = br.readLine()) != null)       { great[count] = new BigInteger(line);         System.out.println(line);         count++;       }   fr.close(); } public static void readPlainText() throws IOException { FileReader fr1 = new FileReader("key.txt");   BufferedReader br1 = new BufferedReader(fr1);   String line;   count1 = 0;  while((line = br1.readLine()) != null)     {       key[count1] = new BigInteger(line);       count1++;     }     fr1.close();   FileReader fr2 = new FileReader("plaintext.txt");   BufferedReader br2= new BufferedReader(fr2);   String line1;   String temp;   int temp2[] = new int[10000];   StringBuilder sb = new StringBuilder();   BigInteger code;   count2 = 0;  while((line1 = br2.readLine()) != null)     {       line1.getChars(0, line1.length(), plaintext, 0);       for(int k = 0; k < line1.length(); k++)       {         temp2[count2] = (int)plaintext[k];         sb.append(temp2[count2]);         count++;       }     }     fr2.close();       BigInteger temp3 = new BigInteger(sb.toString());     code = temp3.multiply(key[0]);               try{     PrintStream out = new PrintStream(new FileOutputStream("encryption.txt"));     out.println(code);     out.close();   } catch (FileNotFoundException e) {   e.printStackTrace();  } } public static void main(String argv[])throws IOException {  great = new BigInteger[100];   key = new BigInteger[5];   String line;   int selection = 0;     System.out.println("Please make your selection.\n 1. Encryption \n 2. Decryption\n");     try{     BufferedReader in = new BufferedReader(new InputStreamReader(System.in));     line = in.readLine();     selection = Integer.parseInt(line);     }     catch (IOException e) {     System.out.println("Unexpected IO Error: " + e);     }     switch(selection) {     case 1:     System.out.println("You have selected Encryption.");     readPlainText();     System.out.println("The encryption.txt file has been created.");     break;     case 2:     System.out.println("You have selected Decryption.");     readBigNumbers();     System.out.println("\n Printing from great[] \n");     for (int i = 0; i < count; i++)       System.out.println(great[i].toString());     break;   }         System.out.println("\n *** Program Finished *** "); } }```
Any help would be great!

Thanks.
• 09-20-2010, 07:39 AM
DarrylBurke
Quote:

Originally Posted by mzjazzygirl
Code:

```static BigInteger base2 = BigInteger.valueOf(base); <--- This is where I am trying to do the conversion, but it is not correct```

Define "not correct"

Your stated problem is with creating a BigInteger. 99% of your code has nothing whatsoever to do with creating a BigInteger.

db
• 09-20-2010, 06:58 PM
mzjazzygirl
Thanks. I know that 99% of the code has nothing to do with the BigInteger, but I didn't want anyone to say, "Where's the rest of your code?"

Also, i'm confused because when I System.out.println statement, the base2 shows 128 as opposed to the BigInteger equivalent.

I hope, I am explaining my problem well.
• 09-20-2010, 07:18 PM
Norm
Quote:

System.out.println statement, the base2 shows 128 as opposed to the BigInteger equivalent.
Can you show what you think the BigInteger equivalent is?
• 09-21-2010, 01:33 AM
mzjazzygirl
Quote:

Originally Posted by Norm
Can you show what you think the BigInteger equivalent is?

Norm,

I don't know what the equivalent will be. My teacher stated that I will have to use the DivideandRemainder() function in Java. The parameter is of a BigInteger, so I am trying to convert my base of 128 (currently stored as an integer) to it's equivalent as a BigInteger.

I hope that makes sense...
• 09-21-2010, 01:40 AM
Norm
Quote:

I am trying to convert my base of 128 (currently stored as an integer) to it's equivalent as a BigInteger.
The value 128 can be stored in many different data types including: unsigned byte, short, int, long and BigInteger. But no matter what data type it is in, its value is still 128.

Why do you think using a BigInteger would change its value?
• 09-21-2010, 02:04 AM
mzjazzygirl
Quote:

Originally Posted by Norm
The value 128 can be stored in many different data types including: unsigned byte, short, int, long and BigInteger. But no matter what data type it is in, its value is still 128.

Why do you think using a BigInteger would change its value?

Norm,

I guess I must have misinterpreted my teacher. He was stating in class that if we tried to put BigInteger base2 = 128, this will not work. I will try the DivideandRemainder() function with the BigInteger.valueOf, I just hope I do not get an error.

Sorry if I am confusing you.

Thanks
• 09-21-2010, 02:10 AM
Norm
Quote:

BigInteger base2 = 128, this will not work
That is correct. You would get an error.
Quote:

incompatible types
found : int
required: java.math.BigInteger
• 09-21-2010, 02:33 AM
mzjazzygirl
Quote:

Originally Posted by Norm
That is correct. You would get an error.

Norm,

Correct, that's what I got! Basically I am doing a homework assignment of a (very basic) Cryptosystem.

I need to use the divideandRemainder() function for this assignment to divide my encryption by the key, and then by base 128.

If I put BigInteger base2 = 128, I don't that will work.

Basically, I am trying to find a way to get 128 as a BigInteger to use this function.
• 09-21-2010, 01:43 PM
Norm
What is wrong with what you originally posted:
BigInteger base2 = BigInteger.valueOf(base);
• 09-23-2010, 12:55 AM
mzjazzygirl
Norm,

I guess there's nothing wrong with it... I just misunderstood.

Now I am having trouble using the divideAndRemainder() function in Java. Can you please take a look when you have a chance?

Here's my code:
Code:

```import java.io.*; import java.math.*; import java.util.*; public class Homework1 { static BigInteger[] great; static BigInteger[] key; static char[] plaintext = new char[1000]; static int count; static int count1; static int count2; static long base = 128; static BigInteger base2 = BigInteger.valueOf(base); public static BigInteger readKey() throws IOException {   FileReader fr1 = new FileReader("key.txt");   BufferedReader br1 = new BufferedReader(fr1);   String line;   count1 = 0;  while((line = br1.readLine()) != null)     {       key[count1] = new BigInteger(line);       //System.out.println("Line... " +line);       count1++;     }     //System.out.println("Let's see... "+ key[0]);     fr1.close(); return key[0]; } public static void readPlainText() throws IOException {   FileReader fr2 = new FileReader("plaintext.txt");   BufferedReader br2= new BufferedReader(fr2);   String line1;   String temp;   int temp2[] = new int[10000];   StringBuilder sb = new StringBuilder();   BigInteger code;   count2 = 0;  while((line1 = br2.readLine()) != null)     {       line1.getChars(0, line1.length(), plaintext, 0);       for(int k = 0; k < line1.length(); k++)       {         temp2[count2] = (int)plaintext[k];         sb.append(temp2[count2]);         count++;       }     }     fr2.close();       BigInteger temp3 = new BigInteger(sb.toString());     code = temp3.multiply(key[0]);               try{     PrintStream out = new PrintStream(new FileOutputStream("encryption.txt"));     out.println(code);     out.close();   } catch (FileNotFoundException e) {   e.printStackTrace();  } } public static void readBigNumbers(BigInteger temp3) throws IOException { FileReader fr = new FileReader("encryption.txt");   BufferedReader br = new BufferedReader(fr);     String line;   //System.out.println("New base from 128, to big integer... "+ base2);   count = 0;   while ((line = br.readLine()) != null)       { great[count] = new BigInteger(line);         //System.out.println(great[count]);         count++;       }   fr.close();   System.out.println("Test 1... "+great[0]); //Encryption file   System.out.println("Test 2... "+temp3); //Key     BigInteger storage = new BigInteger(great[0].toString());   BigInteger k_temp = new BigInteger(temp3.toString());   System.out.println("Prelim test..." +storage);   System.out.println("New prelim test..." +k_temp);     BigInteger storage1;   storage1 = storage.divideAndRemainder(k_temp);   } public static void main(String argv[])throws IOException {  great = new BigInteger[100];   key = new BigInteger[5];   String line;   int selection = 0;   BigInteger temp2;     System.out.println("Please make your selection.\n 1. Encryption \n 2. Decryption\n");     try{     BufferedReader in = new BufferedReader(new InputStreamReader(System.in));     line = in.readLine();     selection = Integer.parseInt(line);     }     catch (IOException e) {     System.out.println("Unexpected IO Error: " + e);     }     switch(selection) {     case 1:     System.out.println("You have selected Encryption.");     temp2 = readKey();     readPlainText();     System.out.println("The encryption.txt file has been created.");     break;     case 2:     System.out.println("You have selected Decryption.");     temp2 = readKey();     readBigNumbers(temp2);   /* System.out.println("\n Printing from great[] \n");     for (int i = 0; i < count; i++)       System.out.println(great[i].toString());*/     break;   }         System.out.println("\n *** Program Finished *** "); } }```
• 09-23-2010, 01:48 AM
Norm
Quote:

I am having trouble using the divideAndRemainder()
Please explain what the trouble is?
• 09-23-2010, 02:36 AM
mzjazzygirl
Hey Norm,

I think I fixed that problem. I am trying to actually implement the divideAndRemainder() function.

Again, I am working on a primitive CryptoSystem program (it's a homework assignment)

I made some changes to my program. First, I looked at another post (under New to Java), dealing with changing chars to ints for ASCII purposes. Anyway, I noticed that the ASCII are in base 10. I need my code to work under base 128, so I changed the chars to ints, and multiplied the ints by 128.

Next, with the divideAndRemainder() function, I am trying to use this in a loop. Basically, I want to keep dividing by the base (in this case 128) until the remainder is 0 (which means i'm at the end of division, nothing else to divide). I am having trouble using the divideAndRemainder() function. I can't find any examples on Google.

When I run my program, and select 2 for Decryption, I get the following:

Code:

```import java.io.*; import java.math.*; import java.util.*; public class Homework1 { static BigInteger[] great; static BigInteger[] key; static char[] plaintext = new char[1000]; static int count; static int count1; static int count2; static long base = 128; static BigInteger base2 = BigInteger.valueOf(base); public static BigInteger readKey() throws IOException {   FileReader fr1 = new FileReader("key.txt");   BufferedReader br1 = new BufferedReader(fr1);   String line;   count1 = 0;  while((line = br1.readLine()) != null)     {       key[count1] = new BigInteger(line);       //System.out.println("Line... " +line);       count1++;     }     //System.out.println("Let's see... "+ key[0]);     fr1.close(); return key[0]; } public static void readPlainText() throws IOException {   FileReader fr2 = new FileReader("plaintext.txt");   BufferedReader br2= new BufferedReader(fr2);   String line1;   String temp;   int temp2[] = new int[10000];   StringBuilder sb = new StringBuilder();   BigInteger code;   count2 = 0;  while((line1 = br2.readLine()) != null)     {       line1.getChars(0, line1.length(), plaintext, 0);       for(int k = 0; k < line1.length(); k++)       {         temp2[count2] = (int)plaintext[k] * 128;         sb.append(temp2[count2]);         count++;       }     }     fr2.close();       BigInteger temp3 = new BigInteger(sb.toString());     code = temp3.multiply(key[0]);               try{     PrintStream out = new PrintStream(new FileOutputStream("encryption.txt"));     out.println(code);     out.close();   } catch (FileNotFoundException e) {   e.printStackTrace();  } } public static void readBigNumbers(BigInteger temp3) throws IOException { FileReader fr = new FileReader("encryption.txt");   BufferedReader br = new BufferedReader(fr);     String line;   //System.out.println("New base from 128, to big integer... "+ base2);   count = 0;   while ((line = br.readLine()) != null)       { great[count] = new BigInteger(line);         //System.out.println(great[count]);         count++;       }   fr.close();   System.out.println("Test 1... "+great[0]); //Encryption file   System.out.println("Test 2... "+temp3); //Key     BigInteger storage = new BigInteger(great[0].toString());   BigInteger k_temp = new BigInteger(temp3.toString());   //System.out.println("Prelim test..." +storage);   //System.out.println("New prelim test..." +k_temp);     BigInteger storage1[] = storage.divideAndRemainder(k_temp);     System.out.println("Quotient... "+storage.divide(k_temp));   /*System.out.println("Integral part... "+storage[1]);   System.out.println("Remainder... "+storage[1]);*/     } public static void main(String argv[])throws IOException {  great = new BigInteger[100];   key = new BigInteger[5];   String line;   int selection = 0;   BigInteger temp2;     System.out.println("Please make your selection.\n 1. Encryption \n 2. Decryption\n");     try{     BufferedReader in = new BufferedReader(new InputStreamReader(System.in));     line = in.readLine();     selection = Integer.parseInt(line);     }     catch (IOException e) {     System.out.println("Unexpected IO Error: " + e);     }     switch(selection) {     case 1:     System.out.println("You have selected Encryption.");     temp2 = readKey();     readPlainText();     System.out.println("The encryption.txt file has been created.");     break;     case 2:     System.out.println("You have selected Decryption.");     temp2 = readKey();     readBigNumbers(temp2);   /* System.out.println("\n Printing from great[] \n");     for (int i = 0; i < count; i++)       System.out.println(great[i].toString());*/     break;   }         System.out.println("\n *** Program Finished *** "); } }```

Code:

```Please make your selection.  1. Encryption  2. Decryption 2 You have selected Decryption. Test 1... 2043609543318642661723365740481725931556904935551036645458759259730927 3543083039960605160641280 Test 2... 21574986861247560388341 Quotient... 94721241614720139521344014080129284096947212416126721369614720142081 4080  *** Program Finished ***```

Thanks
• 09-23-2010, 02:39 AM
mzjazzygirl
Sorry,

Code:

```Please make your selection.  1. Encryption  2. Decryption 2 You have selected Decryption. Encryption... 2043609543318642661723365740481725931556904935551036645458759259730927 3543083039960605160641280 Key... 21574986861247560388341 Quotient... 94721241614720139521344014080129284096947212416126721369614720142081 4080  *** Program Finished ***```
How I received the quotient is by doing encryption/key. My next step is, I want to keep dividing (using the divideAndRemainder() function), and then changing the remainder from a number to a character.

I hope THIS TIDBIT helps.
• 09-23-2010, 03:04 AM
Norm
Sorry, I do not understand most of what you posted or what your problem is.
I can discuss the following:
Quote:

changing the remainder from a number to a character.
Can you show how you want to change a number to a character?
Characters have the same range in values as shorts. That is there are approximately 64K different character values. For example the character '1' has a value of 49.

Please give me some examples, say two columns, one with the number and the other character.
• 09-23-2010, 03:25 AM
mzjazzygirl
OK, I printed this example:
1. Encryption
2. Decryption

1
You have selected Encryption.
Char: Number:
J 74
a 97
s 115
m 109
i 105
n 110
e 101
32
J 74
a 97
c 99
k 107
s 115
o 111
n 110

The encryption.txt file has been created.

*** Program Finished ***

Basically, I changed the chars to its ints equivalent, next, I multiplied each number by 128 (since my base is 128). And then I append the numbers together to create one big number (stored as a BigInteger) next, I multiplied this BigInteger by the cryptokey (this was given) --This is the encryption side

Now, I am working on the decryption side, and I want to get back to my original message. What I am reading in is the BigInteger of the BigInteger * Key. Now I want to first divide by the key, and then continuously divide by 128 (since that's my base) until the remainder is 0 (which means that I have reached the left most letter).

Like I said before, I could not find a divideAndRemainder() example on the web, and I am confused on how to use this function. I want to use the remainder (which is stored as a number) and change this to it's char equivalent (i'm assuming I can use the char cast to do this). I am just confused on how to continuously divide the new quotient and get the remainders.

Does it make sense now?
• 09-23-2010, 03:34 AM
Norm
Quote:

changing the remainder from a number to a character.
but your list shows them in the order: char then number? What you show in the list requires no conversion other than casting. 'n' is 110. A space is 32
Quote:

i'm assuming I can use the char cast to do this
Yes that is how to do it.

Quote:

I could not find a divideAndRemainder() example on the web
Did you read the API doc for that method? It says what it does. Write a 10 line test program and get it to compile and execute. Use the Arrays.toString() method to display the results. Run the test with several different values and you'll see what it does.
• 09-23-2010, 03:41 AM
mzjazzygirl
Norm,

The code output was a sample output of the digits without multiplying them by 128 and appending them to create one BigInteger. I will look for the API now.
• 09-23-2010, 03:42 AM
mzjazzygirl
Where exactly should I go to, looking for this API? Currently I am at the Java SE Tutorials... I think I am in the wrong spot.
• 09-23-2010, 04:05 AM
mzjazzygirl
Norm,

Looked at the API, and I think I know what i'm doing. But I am getting the following error:

Homework1.java:92: incomparable types: java.math.BigInteger and int
while(storage2[1] != 0)
^
1 error

What I am trying to do is while the remainder does not equal zero then keep dividing by the base.

Below is the full code.
Code:

```import java.io.*; import java.math.*; import java.util.*; public class Homework1 { static BigInteger[] great; static BigInteger[] key; static char[] plaintext = new char[1000]; static int count; static int count1; static int count2; static long base = 128; static BigInteger base2 = BigInteger.valueOf(base); public static BigInteger readKey() throws IOException {   FileReader fr1 = new FileReader("key.txt");   BufferedReader br1 = new BufferedReader(fr1);   String line;   count1 = 0;  while((line = br1.readLine()) != null)     {       key[count1] = new BigInteger(line);       //System.out.println("Line... " +line);       count1++;     }     //System.out.println("Let's see... "+ key[0]);     fr1.close(); return key[0]; } public static void readPlainText() throws IOException {   FileReader fr2 = new FileReader("plaintext.txt");   BufferedReader br2= new BufferedReader(fr2);   String line1;   String temp;   int temp2[] = new int[10000];   StringBuilder sb = new StringBuilder();   BigInteger code;   count2 = 0;  while((line1 = br2.readLine()) != null)     {       line1.getChars(0, line1.length(), plaintext, 0);       for(int k = 0; k < line1.length(); k++)       {         temp2[count2] = (int)plaintext[k] * 128;                sb.append(temp2[count2]);         count++;       }     }     fr2.close();       BigInteger temp3 = new BigInteger(sb.toString());     code = temp3.multiply(key[0]);               try{     PrintStream out = new PrintStream(new FileOutputStream("encryption.txt"));     out.println(code);     out.close();   } catch (FileNotFoundException e) {   e.printStackTrace();  } } public static void readBigNumbers(BigInteger temp3) throws IOException { FileReader fr = new FileReader("encryption.txt");   BufferedReader br = new BufferedReader(fr);     String line;   //System.out.println("New base from 128, to big integer... "+ base2);   count = 0;   while ((line = br.readLine()) != null)       { great[count] = new BigInteger(line);         //System.out.println(great[count]);         count++;       }   fr.close();   System.out.println("Test 1... "+great[0]); //Encryption file   System.out.println("Test 2... "+temp3); //Key     BigInteger storage = new BigInteger(great[0].toString());   BigInteger k_temp = new BigInteger(temp3.toString());   //System.out.println("Prelim test..." +storage);   //System.out.println("New prelim test..." +k_temp);     BigInteger storage1[] = storage.divideAndRemainder(k_temp);   BigInteger storage2[] = storage1[0].divideAndRemainder(base2);   while(storage2[1] != 0)   {   System.out.println(storage2[1].toString());   storage1[0].divideAndRemainder(base2);   }     System.out.println("Quotient... "+storage.divide(k_temp));   /*System.out.println("Integral part... "+storage[1]);   System.out.println("Remainder... "+storage[1]);*/     } public static void main(String argv[])throws IOException {  great = new BigInteger[100];   key = new BigInteger[5];   String line;   int selection = 0;   BigInteger temp2;     System.out.println("Please make your selection.\n 1. Encryption \n 2. Decryption\n");     try{     BufferedReader in = new BufferedReader(new InputStreamReader(System.in));     line = in.readLine();     selection = Integer.parseInt(line);     }     catch (IOException e) {     System.out.println("Unexpected IO Error: " + e);     }     switch(selection) {     case 1:     System.out.println("You have selected Encryption.");     temp2 = readKey();     readPlainText();     System.out.println("The encryption.txt file has been created.");     break;     case 2:     System.out.println("You have selected Decryption.");     temp2 = readKey();     readBigNumbers(temp2);   /* System.out.println("\n Printing from great[] \n");     for (int i = 0; i < count; i++)       System.out.println(great[i].toString());*/     break;   }         System.out.println("\n *** Program Finished *** "); } }```
Can you tell me why I am getting this error... I don't understand. How else am I supposed to check for the zero remainder if I can't use the does not equal (!=)
Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last