2015-09-23 199 views
1

我有一個大小爲8的字節數組。 我使用下面的代碼將其轉換爲字符串。 (見下文)。Probem與java,String.getBytes方法

現在,當我使用getBytes方法將字符串再次轉換爲byte []時,結果是荒謬的,這是一個16字節的字節[],與前一個字節數組只有少數(2或3)匹配字節。有人能告訴我我要去哪裏嗎?

byte[] message = new byte[8]; 
//initialize message 
printBytes("message: " + message.length + " = ", message); 
try { 
    String test = new String(message, "utf-8"); 
    System.out.println(test); 
    byte[] f = test.getBytes("utf-8"); 
    Help.printBytes("test = " + f.length, f); 
} catch (UnsupportedEncodingException e1) { 
    // TODO Auto-generated catch block 
    e1.printStackTrace(); 
} 

printBytes功能:

public static void printBytes(String msg, byte[] b){ 
    System.out.print(msg + " = "); 
    for(int i = 0; i < b.length; i++){ 
     System.out.print("" + String.format("%02X", b[i])); 
    } 
    System.out.println("\n"); 
} 

輸出:

message: 8 = = 9A52D5D6C6E999AD 

�R���陭 
test = 16 = EFBFBD52EFBFBDEFBFBDEFBFBDE999AD 
+0

由於字符串編碼Java使用不是8位,它的16位。可能是UNICODE或UTF。不知道哪個。 –

+0

此外,我不認爲將字節數組轉換爲字符串是一個好主意,如果你想它被重現爲字節數組。您可以嘗試轉換爲十六進制字符串。 –

+0

但是在做相反的事情時,它也應該使用相同的編碼。無論如何,我應該得到預期的結果。 – vish4071

回答

6

你原來byte[]有非法字節序列(即序列沒有形成有效的UTF-8字符)。這對於構造函數String(byte[], String)沒有具體說明,但在實現中,這些錯誤字節被替換爲「 」字符,即\uFFFD - UTF-8中的一個三字節字符。你似乎有四個,這就佔了12個字節。

+0

我初始化'byte []'的方式是選擇一個介於0-255之間的隨機值並將其分配給'byte [i]',其中我從[0,8] – vish4071

+0

@ vish4071分配您不能指定隨機值。 UTF8有你需要遵循的規則。 – Kayaman

+0

@ vish4071那麼......不要這樣做,如果你想將你的'byte []'編碼爲utf-8字符串。並非所有字節序列在所有編碼中均有效。如果你想「串化」字節序列化或類似,我會建議base64或類似的東西。 – yshavit

-1
new String(message, "utf-8"); 

該代碼告訴字符串對象,utf-8編碼的消息是。

test.getBytes("utf-8"); 

此代碼的意思是,給我的字符串的字節和編碼爲utf-8編碼的字符串。結果是,你的字符串將被雙utf-8編碼。

只做一次代碼。

String test = new String(message, "utf-8"); 
test.getBytes(); 

樣品爲雙編碼的字符串:

public class Test { 

    public static void main(String[] args) { 
     try { 
      String message = "äöü"; 
      Test.printBytes("java internal encoded: = ", message.getBytes()); 
      Test.printBytes("utf-8 encoded: = ", message.getBytes("utf-8")); 
      // get the string utf-8 encoded and create a new string with the 
      // utf-8 encoded content 
      message = new String(message.getBytes("utf-8"), "utf-8"); 
      Test.printBytes("test get bytes without charset: = ", message.getBytes()); 
      Test.printBytes("test get bytes with charset: = ", message.getBytes("utf-8")); 
      System.out.println(message); 
      System.out.println("double encoded: " + new String(message.getBytes("utf-8"))); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

    } 

    public static void printBytes(String msg, byte[] b) { 
     System.out.print(msg + " = "); 
     for (int i = 0; i < b.length; i++) { 
      System.out.print("" + String.format("%02X", b[i])); 
     } 
     System.out.println("\n"); 
    } 

} 

輸出繼電器:

java internal encoded: = = E4F6FC 
utf-8 encoded: = = C3A4C3B6C3BC 
test get bytes without charset: = = E4F6FC 
test get bytes with charset: = = C3A4C3B6C3BC 

äöü 
double encoded: äöü <-- the java internal encoding is not converted to utf-8, it is double encoded 
+0

這樣回答錯。如果您未將編碼作爲參數傳遞,則將使用平臺默認編碼。由於這可能會導致問題突然你有不同的平臺與不同的編碼,你應該**總是**明確告訴所用的編碼。 – Kayaman

+0

*「你的字符串是雙utf-8編碼的」*對不起,但這聽起來很有趣:P。 – Tom

+0

看看我的帖子,你會看到雙重編碼的字符串! – TwilightTitus