Home > Net >  Loop for taking a string and converting each letter to their ASCII vaules
Loop for taking a string and converting each letter to their ASCII vaules

Time:11-25

I'm looking to take a string for example "password" and convert each letter first to its ASCII value then times it by x 2. I've managed to do this for each letter, for example below the 1st letter p is taken, 112 is found, x 2, giving 224 by using:

int task1 = 0;
String = password;

char ch0 = password.charAt(0);  
int asciivalue0 = ch0;  
task1 = ch0 * 2;

Then I was adding each total together ch0 ch1; and so on, this way repeats a lot of code, I'm trying various different methods to work a loop into this but I'm still learning and at bit of a loss, any pointers would greatly be appreciated.

CodePudding user response:

I'm sorry if not understanding the last part of the question because i don't know what u mean about adding them together?

but the i think this may help you.

String password = "password";
char[] chars = password.toCharArray();

    for (int i = 0; i < password.length(); i  ) {
        chars[i] = (char) ((int) chars[i]*2);
        System.out.println((int)chars[i]   " "   chars[i]);
    }

this will give you a hint how it repeated a long with all variable if you explain to me what u mean by last step i can edit the answer.

  • Related