如何在java中加密字符串
我想通过向字符串中的每个字符添加+1 ascii来加密字符串 这是我的尝试如何在java中加密字符串,java,Java,我想通过向字符串中的每个字符添加+1 ascii来加密字符串 这是我的尝试 public static string encrypt(string str){ for(int i = 0; i < str.length(); i++){ int x = str.charAt(i) ; x = x + 1; } //现在,如何通过向每个字符添加1来加密字符串,从而完成此循环以生成新字符串?我建议您尝试以下操作: Public static string encrypt(s
public static string encrypt(string str){
for(int i = 0; i < str.length(); i++){
int x = str.charAt(i) ;
x = x + 1;
}
//现在,如何通过向每个字符添加1来加密字符串,从而完成此循环以生成新字符串?我建议您尝试以下操作:
Public static string encrypt(string str){
String result = "";
for(int i=0; i<str.length() ; i++){
int x = str.charAt(i) ;
x = x+1 ;
result+= Character.toString((char)x);
}
return result;
}
很好的解决方案。尽管使用StringBuilder会更好,以避免每次向结果字符串添加字符时复制整个结果字符串。是的,速度会快一点。请注意,在诸如char之类的整数类型上+1会产生从32767到-32768或从65535到0的循环。此外,我相信java遵循其字符集的直接16位编码,UCS的BMP表示+1,但不一定会产生字符集中真正有效字符的代码点编号。例如,请参见中的灰色方框。所有这些东西可能会产生有趣的结果。