我在java.I中新建一個帶有十六進制值的文本文檔,我試圖讀取它並將其轉換爲字節數組。但對於十六進制值等8,d,11,0,解析即時得到錯誤的值對於E4爲-28代替228 如何可以克服該轉換錯誤時E4 ....將十六進制轉換爲java中的字節
FileInputStream fstream = new FileInputStream("C:/Users/data.txt");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(newInputStreamReader(in,"UTF-8"));
byte[] bytes = new byte[1024];
String str;
int i=0;
while ((str = br.readLine()) != null)
{
bytes[i]= (byte) (Integer.parseInt(str,16) & 0xFF);
i++;
}
byte[] destination = new byte[i];
System.arraycopy(bytes, 0, destination, 0, i);
br.close();
return destination;
Java中的字節已簽名。它的範圍是 - '[-128,127]','127'後面的值是負值。因此'228'是'-28'。 –
請不要使用DataInputStream來閱讀文本http://vanillajava.blogspot.co.uk/2012/08/java-memes-which-refuse-to-die.html –