2015-06-08 23 views
0

以十六進制格式獲取一系列字節值,並將它們寫入字節流。 「01 02 1a」=>將字節0x01 0x02 0x1a寫入字節流。十六進制格式的字節值並將它們寫入字節流?

這是什麼意思?

+0

「十六進制格式的字節值的序列」,這是十六進制格式的字節值的字符串或字節數組? 「寫入字節流」,你是指一個文件? – Shar1er80

+0

十六進制格式字節值的字符串「01,02,1a」 –

+0

你的字節流是什麼?您是使用FileOutputStream將結果寫入文件還是想將其寫爲純文本以顯示0x01 0x02 0x1a?我問,因爲文件中的實際字節值在值之前不會有「0x」。 – Shar1er80

回答

0

這裏是一個可能的解決方案:

String hex = "01 02 1a"; 

    // Remove spaces 
    hex = hex.replace(" ", ""); 

    // Array containing bytes 
    byte[] bytes = new byte[hex.length()/2]; 

    int k = 0; 
    for(int i=0; i < hex.length(); i = i +2) { 
     // Read and parse each byte 
     int b = Integer.parseInt(hex.substring(i, i + 2), 16); 
     bytes[k] = (byte) b; 
     k++; 
    } 

    // Write bytes to an outputStram 
    OutputStream out; 
    for(Byte b: bytes) { 
     out.write(b); 
    } 
+0

0x01 0x02 0x1a這應該是輸出 –

+0

是的,這意味着你已經寫入每個字節到一個outputStream:'for(Byte b:bytes)outputStream.write(b);' –

+0

對不起,請您舉例 –

相關問題