# Thread: Converting an integer into a BigInteger

1. Member 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;
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

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
{

String line;

count1 = 0;
{
key[count1] = new BigInteger(line);
count1++;
}
fr1.close();

String line1;
String temp;
int temp2[] = new int;
StringBuilder sb = new StringBuilder();
BigInteger code;

count2 = 0;
{
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);

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;
key = new BigInteger;
String line;
int selection = 0;

try{
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.");
System.out.println("The encryption.txt file has been created.");
break;

case 2:
System.out.println("You have selected Decryption.");
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.  Reply With Quote

2. ##  Originally Posted by mzjazzygirl Java 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  Reply With Quote

3. Member 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.  Reply With Quote

4. ## System.out.println statement, the base2 shows 128 as opposed to the BigInteger equivalent.
Can you show what you think the BigInteger equivalent is?  Reply With Quote

5. Member Join Date
Sep 2010
Posts
16
Rep Power
0

##  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...  Reply With Quote

6. ## 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?  Reply With Quote

7. Member Join Date
Sep 2010
Posts
16
Rep Power
0

##  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  Reply With Quote

8. ## BigInteger base2 = 128, this will not work
That is correct. You would get an error.
incompatible types
found : int
required: java.math.BigInteger  Reply With Quote

9. Member Join Date
Sep 2010
Posts
16
Rep Power
0

##  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.  Reply With Quote

10. ## What is wrong with what you originally posted:
BigInteger base2 = BigInteger.valueOf(base);  Reply With Quote

11. Member 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;
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
{
String line;
count1 = 0;
{
key[count1] = new BigInteger(line);
//System.out.println("Line... " +line);
count1++;
}
//System.out.println("Let's see... "+ key);
fr1.close();
return key;
}

public static void readPlainText() throws IOException
{
String line1;
String temp;
int temp2[] = new int;
StringBuilder sb = new StringBuilder();
BigInteger code;

count2 = 0;
{
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);

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

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); //Encryption file
System.out.println("Test 2... "+temp3); //Key

BigInteger storage = new BigInteger(great.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;
key = new BigInteger;
String line;
int selection = 0;
BigInteger temp2;

try{
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.");
System.out.println("The encryption.txt file has been created.");
break;

case 2:
System.out.println("You have selected Decryption.");
/* 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 *** ");
}

}```  Reply With Quote

12. ## I am having trouble using the divideAndRemainder()
Please explain what the trouble is?  Reply With Quote

13. Member 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;
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
{
String line;
count1 = 0;
{
key[count1] = new BigInteger(line);
//System.out.println("Line... " +line);
count1++;
}
//System.out.println("Let's see... "+ key);
fr1.close();
return key;
}

public static void readPlainText() throws IOException
{
String line1;
String temp;
int temp2[] = new int;
StringBuilder sb = new StringBuilder();
BigInteger code;

count2 = 0;
{
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);

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

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); //Encryption file
System.out.println("Test 2... "+temp3); //Key

BigInteger storage = new BigInteger(great.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);
System.out.println("Remainder... "+storage);*/

}

public static void main(String argv[])throws IOException
{  great = new BigInteger;
key = new BigInteger;
String line;
int selection = 0;
BigInteger temp2;

try{
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.");
System.out.println("The encryption.txt file has been created.");
break;

case 2:
System.out.println("You have selected Decryption.");
/* 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  Reply With Quote

14. Member Join Date
Sep 2010
Posts
16
Rep Power
0

## Sorry,

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 ***```
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.  Reply With Quote

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.
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.  Reply With Quote

16. Member Join Date
Sep 2010
Posts
16
Rep Power
0

## 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?  Reply With Quote

17. ## 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
i'm assuming I can use the char cast to do this
Yes that is how to do it.

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.  Reply With Quote

18. Member 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.  Reply With Quote

19. Member 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.  Reply With Quote

20. Member 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 != 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;
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
{
String line;
count1 = 0;
{
key[count1] = new BigInteger(line);
//System.out.println("Line... " +line);
count1++;
}
//System.out.println("Let's see... "+ key);
fr1.close();
return key;
}

public static void readPlainText() throws IOException
{
String line1;
String temp;
int temp2[] = new int;
StringBuilder sb = new StringBuilder();
BigInteger code;

count2 = 0;
{
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);

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

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); //Encryption file
System.out.println("Test 2... "+temp3); //Key

BigInteger storage = new BigInteger(great.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.divideAndRemainder(base2);
while(storage2 != 0)
{
System.out.println(storage2.toString());

storage1.divideAndRemainder(base2);
}

System.out.println("Quotient... "+storage.divide(k_temp));
/*System.out.println("Integral part... "+storage);
System.out.println("Remainder... "+storage);*/

}

public static void main(String argv[])throws IOException
{  great = new BigInteger;
key = new BigInteger;
String line;
int selection = 0;
BigInteger temp2;

try{
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.");
System.out.println("The encryption.txt file has been created.");
break;

case 2:
System.out.println("You have selected Decryption.");
/* 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 (!=)  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•