我將一個byte []數組轉換爲字符串。然後將字符串轉換回byte []數組。然後在檢查如果兩個數組相等, - 我讓他們不相等:
字符串到byte和viceversa在java中
byte[] ciphertext2=c.doFinal(username.getBytes("utf-8"));
//JUST CHECKING IGNORE
String qaz=new String(ciphertext2,"utf-8");
//qaz=qaz+"1";
System.out.println("just chekcing------------------------"+qaz);
byte[] ciphertext3=qaz.getBytes("utf-8");
if(Arrays.equals(ciphertext2,ciphertext3))
{
System.out.println("just chekcing they are equal------------------------");
}
else
System.out.println("just chekcing they are not equal------------------------");<br>
OUTPUT:
just chekcing they are not equal--------------------
爲什麼它不工作?
編輯
當使用java的Base64時,它工作得很好。但是,爲什麼直接將字節轉換爲字符串並反之亦然?將字符串轉換爲字節數組時實際發生了什麼?反之亦然?
您的字節可能無效utf-8。當它們被解析爲utf-8字符串時,無效序列會變成有效的utf-8序列,因此字節數組是不同的。 Base64保證是有效的utf-8(它畢竟是ascii的超集)。 – bdares 2012-04-18 05:50:46
@bdares:如何將字節數組轉換爲字符串?裏面會發生什麼? – Ashwin 2012-04-18 05:54:28
如果我對用戶名的值進行了硬編碼,我會將其設置爲true。 – UVM 2012-04-18 05:57:57