Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 14 of 14

Thread: How do I convert a char into a BigInteger and vice versa?

  1. #1
    Member
    Join Date
    Sep 2013
    Posts
    64
    Thanks
    24
    Thanked 0 Times in 0 Posts

    Default How do I convert a char into a BigInteger and vice versa?

    I'm trying to do an encryption. I have this message: "hello world"

    I am supposed to encrypt and decrypt that character by character.

    So, the letter 'h' as a char may equal 97, and so on.

    I want to make 97 a BigInteger, and then perform arithmetic on that to encrypt it.

    I can turn the String into a char array.
    I can turn the char array into an int array.
    I cannot turn the int array into a BigInteger array.

    Mind you, I need to use the BigInteger.modPow(e,n) function in order to perform the encryption, so it must ultimately become a BigInteger.

    And, even if I did make the conversion, how would I convert it back?

    Looked everywhere online for over an hour, couldn't find anything that makes sense.

    Thanks in advance.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How do I convert a char into a BigInteger and vice versa?

    I am supposed to encrypt and decrypt that character by character.
    I don't understand why you are using BigInteger if the task does it one character at a time.
    What is done with the encrypted character?
    How are the encrypted characters saved? In an array?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Sep 2013
    Posts
    64
    Thanks
    24
    Thanked 0 Times in 0 Posts

    Default Re: How do I convert a char into a BigInteger and vice versa?

    Okay, for encryption, we use this formula:

    c = m^e mod n
    m is the "message".
    We need a BigInteger.modPow(e,n) to calculate this.

    So, the 'm' must be a BigInteger.

    I intend to convert each character of the String into a BigInteger and perform the arithmetic, creating an array of BigIntegers the size of the length of the original message, where each entry in the array represents a character of the message.

    And then convert back from that. Make sense?

    --- Update ---

    Though I'm sure there's a simpler way to do it character by character!

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How do I convert a char into a BigInteger and vice versa?

    What does the BigInteger constructor take to create an instance of BigInteger?
    What technique can you use to convert a character to what the constructor takes?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Sep 2013
    Posts
    64
    Thanks
    24
    Thanked 0 Times in 0 Posts

    Default Re: How do I convert a char into a BigInteger and vice versa?

    Thanks for the response!

    I found this:

    BigInteger(String val)
    Translates the decimal String representation of a BigInteger into a BigInteger.

    But I don't really know what the "decimal String representation is", and that doesn't seem to convert character by character.


    I also found this:

    BigInteger(byte[] val)
    Translates a byte array containing the two's-complement binary representation of a BigInteger into a BigInteger.

    But the two's complement binary representation kind of scares me. Plus I don't even know what a Byte array is....though it seems like that's the one I should use.

    But...I'm creating a single BigInteger, not an array of BigIntegers, right? How exactly does that represent the String?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How do I convert a char into a BigInteger and vice versa?

    BigInteger(String val)
    Translates the decimal String representation of a BigInteger into a BigInteger.
    That looks promising. Go read the details for that one.

    Try some experiments using Strings of decimal digits: "56" or "65" and see what it does when you use some of the methods to add etc two BIs
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Sep 2013
    Posts
    64
    Thanks
    24
    Thanked 0 Times in 0 Posts

    Default Re: How do I convert a char into a BigInteger and vice versa?

    So I can pass the BigInteger function a String....but the String has to contain numbers within it?

    But I want to pass a String that is a message, not a String of numbers.

    Do I need to take the message, make it an array of chars, make the chars into ints, and then make the char array into a String? That seems pretty convoluted, and it doesn't convert character by character.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How do I convert a char into a BigInteger and vice versa?

    pass the BigInteger function a String....but the String has to contain numbers within it
    Is that what the doc said? Did you write a small program to create two BIs , add them together and print out the results to see how they worked?

    The next step will be to convert a character to its int value and then that to a String of digits for the constructor.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Sep 2013
    Posts
    64
    Thanks
    24
    Thanked 0 Times in 0 Posts

    Default Re: How do I convert a char into a BigInteger and vice versa?

    Yeah, I made that test program and it worked only when I passed a String like "45" or "439", not when I passed "hello" or "world".

    I also have this code, which is converting character to int value and then that to a String of digits for the constructor, but it won't compile:

    public void Encrypt()
            {
                    int[] numArray = new int[message.length()];
                    char[] charArray = new char[message.length()];
     
                    charArray = message.toCharArray();
     
                    System.out.println("Encrypted message: ");
     
                    for (int i = 0; i < message.length(); i++)
                    {
                            numArray[i] = charArray[i];
                    }
     
                    String str = Arrays.toString(numArray);
                    BigInteger BI = new BigInteger(str);
     
     
            }

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How do I convert a char into a BigInteger and vice versa?

    it won't compile:
    Please copy the full text of the error message and paste it here. It has important info about the error.

    Have you tried a simple test program creating some BIs and using some methods and printing out the results?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Sep 2013
    Posts
    64
    Thanks
    24
    Thanked 0 Times in 0 Posts

    Default Re: How do I convert a char into a BigInteger and vice versa?

    I got it compiling, but now it has this error during runtime:
    HTML Code:
    Encrypted message:
    Exception in thread "main" java.lang.NumberFormatException: For input string: "[104, 1"
            at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
            at java.lang.Integer.parseInt(Integer.java:492)
            at java.math.BigInteger.<init>(BigInteger.java:338)
            at java.math.BigInteger.<init>(BigInteger.java:476)
            at RSACipher1.Encrypt(RSACipher1.java:134)
            at RSACipher1.main(RSACipher1.java:44)
    And yeah, I'll do some testing right now.

    --- Update ---

    And this is line 44:

    BigInteger BI = new BigInteger(str);

    --- Update ---

    Okay, and I tried outputting the String "str" to the screen...and I got this:
    str = [108, 111, 107]

    So obviously that's no good lol.

    But then how am I supposed to be making these conversions???

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How do I convert a char into a BigInteger and vice versa?

    how am I supposed to be making these conversions???
    I suppose, one char at a time.

    For testing I'd use a very short String, 1 or two characters, so the results can be easily verified manually.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Member
    Join Date
    Sep 2013
    Posts
    64
    Thanks
    24
    Thanked 0 Times in 0 Posts

    Default Re: How do I convert a char into a BigInteger and vice versa?

    Okay, I finally got the little bastard to work.

    Thanks for all the help.

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How do I convert a char into a BigInteger and vice versa?

    Glad you got it going.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Sequences (convert char[] to String) [Replacing String with Character]
    By tpolwort in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 25th, 2013, 02:56 PM
  2. Celsius to fahrenheit vice versa
    By Onlyname in forum What's Wrong With My Code?
    Replies: 5
    Last Post: September 16th, 2013, 11:39 PM
  3. Gregorian to Julian and vise versa
    By heythisgreg in forum Java Theory & Questions
    Replies: 4
    Last Post: January 27th, 2013, 07:01 PM
  4. How to convert char to string by using method invocation?
    By Akirien in forum Object Oriented Programming
    Replies: 1
    Last Post: August 26th, 2012, 05:49 AM
  5. Convert CHAR to STRING
    By fh84 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: October 29th, 2009, 09:21 PM