Results 1 to 20 of 28
 09202010, 07:38 AM #1Member
 Join Date
 Sep 2010
 Posts
 16
 Rep Power
 0
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:
Java 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 *** "); } }
Thanks.
 09202010, 08:39 AM #2
Define "not correct"
Your stated problem is with creating a BigInteger. 99% of your code has nothing whatsoever to do with creating a BigInteger.
Recommended reading: How To Ask Questions The Smart Way
db
 09202010, 07:58 PM #3Member
 Join Date
 Sep 2010
 Posts
 16
 Rep Power
 0
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.
 09202010, 08:18 PM #4System.out.println statement, the base2 shows 128 as opposed to the BigInteger equivalent.
 09212010, 02:33 AM #5Member
 Join Date
 Sep 2010
 Posts
 16
 Rep Power
 0
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...
 09212010, 02:40 AM #6I am trying to convert my base of 128 (currently stored as an integer) to it's equivalent as a BigInteger.
Why do you think using a BigInteger would change its value?
 09212010, 03:04 AM #7Member
 Join Date
 Sep 2010
 Posts
 16
 Rep Power
 0
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
 09212010, 03:10 AM #8BigInteger base2 = 128, this will not work
incompatible types
found : int
required: java.math.BigInteger
 09212010, 03:33 AM #9Member
 Join Date
 Sep 2010
 Posts
 16
 Rep Power
 0
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.
 09212010, 02:43 PM #10
What is wrong with what you originally posted:
BigInteger base2 = BigInteger.valueOf(base);
 09232010, 01:55 AM #11Member
 Join Date
 Sep 2010
 Posts
 16
 Rep Power
 0
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:
Java 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 *** "); } }
 09232010, 02:48 AM #12I am having trouble using the divideAndRemainder()
 09232010, 03:36 AM #13Member
 Join Date
 Sep 2010
 Posts
 16
 Rep Power
 0
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:
Java 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 *** "); } }
Java 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
 09232010, 03:39 AM #14Member
 Join Date
 Sep 2010
 Posts
 16
 Rep Power
 0
Sorry,
The output really doesn't help you...
Java Code:Please make your selection. 1. Encryption 2. Decryption 2 You have selected Decryption. Encryption... 2043609543318642661723365740481725931556904935551036645458759259730927 3543083039960605160641280 Key... 21574986861247560388341 Quotient... 94721241614720139521344014080129284096947212416126721369614720142081 4080 *** Program Finished ***
I hope THIS TIDBIT helps.
 09232010, 04:04 AM #15
Sorry, I do not understand most of what you posted or what your problem is.
I can discuss the following:
changing the remainder from 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.
 09232010, 04:25 AM #16Member
 Join Date
 Sep 2010
 Posts
 16
 Rep Power
 0
OK, I printed this example:
Please make your selection.
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?
 09232010, 04:34 AM #17changing the remainder from a number to a character.
i'm assuming I can use the char cast to do this
I could not find a divideAndRemainder() example on the web
 09232010, 04:41 AM #18Member
 Join Date
 Sep 2010
 Posts
 16
 Rep Power
 0
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.
 09232010, 04:42 AM #19Member
 Join Date
 Sep 2010
 Posts
 16
 Rep Power
 0
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.
 09232010, 05:05 AM #20Member
 Join Date
 Sep 2010
 Posts
 16
 Rep Power
 0
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.
Java 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 *** "); } }
Similar Threads

BigInteger
By windows.login in forum New To JavaReplies: 8Last Post: 07132010, 02:10 PM 
convert unsigned integer to signed integer in java?
By diskhub in forum New To JavaReplies: 6Last Post: 05172010, 01:50 AM 
My own BigInteger class. Need help.
By Dinde in forum New To JavaReplies: 2Last Post: 01272010, 09:49 PM 
[SOLVED] [newbie] converting Integer to java.lang.Enum.Modifier
By jon80 in forum New To JavaReplies: 24Last Post: 05182009, 11:23 AM 
how to convert from BigInteger to Hex
By nanaji in forum Advanced JavaReplies: 10Last Post: 05222008, 01:44 PM
Bookmarks