首先做你的字節數組正確的琴絃的皈依,通過做
String line= new String(Arrays.toString(your_array))
然後把它和使用下面
public static byte[] StringToByteArray(String line)
{
String some=line.substring(1, line.length()-1);
int element_counter=1;
for(int i=0; i<some.length(); i++)
{
if (some.substring(i, i+1).equals(","))
{
element_counter++;
}
}
int [] comas =new int[element_counter-1];
byte [] a=new byte[element_counter];
if (a.length==1)
{
a[0]= Byte.parseByte(some.substring(0));
}
else
{
int j=0;
for (int i = 0; i < some.length(); i++)
{
if (some.substring(i, i+1).equals(","))
{
comas[j]=i;
j++;
}
}
for (int i=0; i<element_counter; i++)
{
if(i==0)
{
a[i]=Byte.parseByte(some.substring(0, comas[i]));
}
else if (i==element_counter-1)
{
a[i]=Byte.parseByte(some.substring(comas[comas.length-1]+2));
}
else
{
a[i]=Byte.parseByte(some.substring(comas[i-1]+2, comas[i]));
}
}
}
return a;
}
函數用於轉換字符串的字節數組使用: 字符串stringToConvert =「這個字符串長度爲76個字符,將被轉換爲一個字節數組」。 byte [] theByteArray = stringToConvert.getBytes(); – 2011-03-31 12:39:15
W0JAM2U0YTlhN2Q =是[B @ 3e4a9a7d的Base64,它看起來像是Java中字節數組的字符串表示。如果您自己正在執行字節操作,請仔細檢查它,因爲您可能正在發送錯誤的數據。 – mdrg 2011-03-31 12:41:41
out是我的字節數組,打印時給出了我的示例輸出的第一行。 out.toString()肯定給我[B @ 3e4a9a7d – Mitch 2011-03-31 12:57:41