Converting ASCII code to characters does not work

First of all, I'm new to Android development. I would like to know why my app crashes when I try to convert 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?

Author: ramaral, 2014-02-04

1 answers

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

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

You are iterating one more character. The code should be:

for (int i= 0; i < str.length(); i++)
 3
Author: bfavaretto, 2014-02-04 20:45:31