Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Convereting ascii codes to text in java

Options
  • 02-08-2005 10:01am
    #1
    Closed Accounts Posts: 999 ✭✭✭


    I've been searching around everywhere for this and I can't find a solution.
    I'm writing a basic password generator. I have it set to generate random numbers which correspond to the ascii table, numbers, upper case and lower case. What I can't do is create the characters corresponding to the numbers I've generated. Any ideas?


Comments

  • Closed Accounts Posts: 19,777 ✭✭✭✭The Corinthian


    Raz wrote:
    I've been searching around everywhere for this and I can't find a solution.
    How many miliseconds were you searching for?

    http://www.google.ie/search?q=Converting+ascii+codes+to+text+in+java


  • Closed Accounts Posts: 999 ✭✭✭Raz


    Yes I know that you can use \u but the problem is doing this dynamicly,
    result = rand1.nextInt(129);
    temp = "\u00" + result;
    
    result is an 'int', rand1 is a 'Random' object and temp is a String. This doesn't work because \u00 is an illegal unicode escape. The problem is I don't know how I can do it without concatinating the \u escape with the result object.
    I think my best bet might be using a lookup table.


  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    that should work except you would have to express it as a hex format.

    so you get

    \u0081 instead of \u00129


  • Closed Accounts Posts: 19,777 ✭✭✭✭The Corinthian


    Raz wrote:
    result is an 'int', rand1 is a 'Random' object and temp is a String. This doesn't work because \u00 is an illegal unicode escape. The problem is I don't know how I can do it without concatinating the \u escape with the result object.
    I think my best bet might be using a lookup table.
    Ahhhh... I see now. Apologies, your question seemed to be different. Having said that, why haven’t you tried using chr instead of \n?


  • Closed Accounts Posts: 999 ✭✭✭Raz


    I looked at using characters but I decided to go with the lookup table option before going into chars to deep. The lookup table works as a nice workaround and is probably more efficient anyway so I'll stick with it.
    Thanks for your help guys.
    Raz


  • Advertisement
  • Registered Users Posts: 441 ✭✭robfitz


    Here is some example code which should do the trick.

    Cast the random result to char.
    StringBuffer password = new StringBuffer();
    for (int i = 0; i < 8; i++) {
        int result = rand1.nextInt(129);
        password.append((char)result);
    }
    System.out.println("ASCII password " + password);
    

    Use the random result as the index into a lookup table.
    char lookuptable[] = {'A', ..., 'Z', 'a', ..., 'z', '0', ..., '9', ...};
    StringBuffer password = new StringBuffer();
    for (int i = 0; i < 8; i++) {
        int result = rand1.nextInt(lookuptable.length);
        password.append(lookuptable[result]);
    }
    System.out.println("Lookup Table password " + password);
    


  • Closed Accounts Posts: 1,502 ✭✭✭MrPinK


    Not a fan of huge lookup tables myself. Converting ints to chars is relatively simple. If you have values from 0-25, you can get corresponding letters by simply adding 'a' (it actually adds the ascii value of that character) to it. This example is slightly more difficult seeing as you want to map to 3 different range of values in the ascii table.
    int result = rand1.nextInt(62); // the number of valid characters 
    if(result < 10) 
        password += (char)(result + '0');  // map first 10 values to numbers
    else if(result < 36) 
        password += (char)(result - 10 + 'A'); // map next 26 values to uppercase letters
    else 
        password += (char)(result - 36 + 'a'); // map next 26 values to lowercase letters
    
    In practise it'd be much nicer without the magic numbers


  • Registered Users Posts: 4,003 ✭✭✭rsynnott


    Ah, lookup tables. See http://thedailywtf.com for more wonderful programming ideas :)


Advertisement