1
First of all, I’m new to developing Android. I would like to know why my application hangs 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 at 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()));
}
}
});
There’s something wrong?
You are using
setOnClickListener
in a method that returns a string???– Androiderson
You need to debug your code. See what value the expression
(int)str.charAt(i)
has when the problem happens.– Oralista de Sistemas
Please note that
String
is a sequence of UTF-16, not ASCII. The most important difference is that any ASCII sequence is valid, but there are UTF-16 sequences that are not -- it is not possible to reorganize or do arithmetic with UTF-16 characters arbitrarily.– Daniel C. Sobral