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 5 of 5

Thread: how can i convert unicode to string?

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Exclamation how can i convert unicode to string?

    im creating a simple encryption program that can encrypt and decrypt sentences entered by converting all the letters to unicode subtracting or adding a "key" to it then converting it back to a letter.
    right now the program works in converting to unicode and adding or subtracting the key. but i cant for the life of me figure out how to convert the unicode back into a letter. any help with this would be much appreciated
    so far i have tried:
    Character.getName(unicode);
    Character.toChars(unicode);
    and a couple others i cant recall XD


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: how can i convert unicode to string?

    program works in converting to unicode
    how to convert the unicode back into a letter
    Can you post a small, complete program that compiles, executes and shows the problem you are having working with unicode.

    Java characters are unicode.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2012
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: how can i convert unicode to string?

    ok here is my program with the getName and toChars examples i said, one for encryption and one for decryption.
    and what i mean is that each letter in java has a corresponding unicode, (example " "(space) has the unicode of 32) what my program does is turn each letter to its unicode value subtract a key chosen by the user and then convert it back to the letter it corresponds to, however im having difficulty with the second part of turning the unicode back into the corresponding letter. (also i have limiters on this program where if the letter is a space i leave it without encrypting it and if the encryption makes a letter go too high or too low it loops back around to the beginning of the alphabet, example the letter is Z and the key for encryption is 1 (meaning make Z go up 1 letter) instead of going to the next unicode after Z (i beleive its "/") it loops back around to A.


    //made by: Matteo Zecconi
    //date: January, 03, 2013
    //This program will encrypt or decrypt a phrase using the simple encryption method of rotating letters.
    package simpleencryption;
     
    import javax.swing.JOptionPane;
     
    /**
     *
     * @author Matteo
     */
    public class SimpleEncryption {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
     
            //declare the variables
            String Rotation = "0";//the rotation amount that the user enters
            String phrase = "";//the phrase that the user wants encrypted or decryted
            int unicode = 0;//used to determine the unicode of the phase
            String phraseEn = "";//the phrase in unicode form encrypted
            String choice = "0";//used to determine if the user wants to encrypt or decrypt a message
            int i = 0;//used in the for loop
     
            phrase = JOptionPane.showInputDialog("This program will encrypt or decrypt a phrase using the\nsimple encryption method of rotating letters.\n\nPlease enter a phrase");
            phrase = phrase.toUpperCase();
            choice = JOptionPane.showInputDialog("1 - Encryption \n2 - Decryption");
            Rotation = JOptionPane.showInputDialog("Enter the rotation amount (1-25)");
     
            //decryption
            if (choice.equals("2")) {
                if (Integer.parseInt(Rotation) > 25 || Integer.parseInt(Rotation) < 1) {
                    JOptionPane.showMessageDialog(null, "The rotation amount entered is not valid");
                } else {
                    for (i = 0; i < phrase.length(); i++) {
                        unicode = phrase.codePointAt(i);
                        if (unicode == 32) {
                            phraseEn += " ";
                        } else {
                            unicode = unicode - Integer.parseInt(Rotation);
                            if (unicode < 65) {
                                unicode = unicode + 26;
                            }
                            phraseEn += Character.getName(unicode);
                        }
                    }
                    JOptionPane.showMessageDialog(null, "The original Phrase is: " + phrase + "\nThe decrypted phrase is: " + phraseEn);
                }
            } else {
    //encryption
     
                if (Integer.parseInt(Rotation) > 25 || Integer.parseInt(Rotation) < 1) {
                    JOptionPane.showMessageDialog(null, "The rotation amount entered is not valid");
                } else {
                    for (i = 0; i < phrase.length(); i++) {
                        unicode = phrase.codePointAt(i);
                        if (unicode == 32) {
                            phraseEn += " ";
                        } else {
                            unicode = unicode + Integer.parseInt(Rotation);
                            if (unicode > 90) {
                                unicode = unicode - 26;
                            }
                            phraseEn += Character.toChars(unicode);
                        }
                    }
                    JOptionPane.showMessageDialog(null, "The original Phrase is: " + phrase + "\nThe encrypted phrase is: " + phraseEn);
                }
            }
        }
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: how can i convert unicode to string?

    One problem I see in the code is the use of too many magic numbers: 65, 32 90. char variables can be used in expressions just like int or short. For example: (char)65 is 'A'. (char)90 is 'Z'

    You can do arithmetic directly with char values: 'C' - 2 = 'A' or 'x' + 1 = 'y'
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    atrixes (January 3rd, 2013)

  6. #5
    Junior Member
    Join Date
    Dec 2012
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: how can i convert unicode to string?

    OMG java does that!!!?? well then thats all i need, thank you very much. im used to coding in actionscript where it couldnt do that you had to convert it to unicode and then back to its letter. thank you very much norm for helping me

Similar Threads

  1. Replies: 2
    Last Post: October 22nd, 2011, 01:34 AM
  2. I need to convert String into array
    By talha07 in forum Collections and Generics
    Replies: 1
    Last Post: October 9th, 2011, 01:58 PM
  3. How can I convert a String to Set<String>? Is it possible?
    By noFear in forum Java Theory & Questions
    Replies: 2
    Last Post: August 25th, 2010, 09:03 AM
  4. How to convert a String into an Hexadecimal ?
    By lumpy in forum Java Theory & Questions
    Replies: 2
    Last Post: February 16th, 2010, 05:01 PM
  5. Conversion of string into integer in Java
    By JavaPF in forum Java Programming Tutorials
    Replies: 17
    Last Post: January 23rd, 2010, 09:33 AM