Conversion of ASCII code to characters does not work

1

First of all, I'm new to Android development. I would like to know why my application crashes when I try to convert the ASCII code to characters.

private String crip(String str, String psw) {
    int code = 0;
    String full_word="";
    for (int i= 0; i <= str.length(); i++) {
        code=(int)str.charAt(i); // Crashes aqui (eu acho)
        full_word+=code;
    }
    return full_word;
}

And in the onClick event:

crip.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        if (!psw.getText().toString().isEmpty() && !str.getText().toString().isEmpty()) {
            out.setText(crip(str.getText().toString(), psw.getText().toString()));
        }

    }
});

Is there something wrong?

    
asked by anonymous 04.02.2014 / 21:30

1 answer

3

My Java is very weak, but I see a problem here:

for (int i= 0; i <= str.length(); i++)

You are iterating an extra character. The code should be:

for (int i= 0; i < str.length(); i++)
    
04.02.2014 / 21:45