我有一個名爲byteArr[]
的字節數組,我需要從中移除前4個字節。我的代碼如下所示。在這裏,我使用字節數組來存儲輸入字符串。我得到了一些不需要的字節,即從第五位起不需要前四個字節是正確的。我的程序是使用rfid機器從尊重的rfid標籤中獲取id。如何從Java中的字節數組中刪除前4個字節?
public class Serverc {
final protected static char[] hexArray = "ABCDEF".toCharArray();
public static String bytesToHex(byte[] bytes) {
char[] hexChars = new char[bytes.length * 2];
for (int j = 0; j < bytes.length; j++) {
int v = bytes[j] & 0xFF;
hexChars[j * 2] = hexArray[v >>> 4];
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
}
return new String(hexChars);
}
public static void connection() throws IOException {
ServerSocket ss = new ServerSocket(9888);//exce
ss.setSoTimeout(300000000);//exce
System.out.println("Waiting for client on port " + ss.getLocalPort() + "...");
while (true) {
Socket server = ss.accept();//exce
System.out.println("Just connected to " + server.getRemoteSocketAddress());
int available = 0;
DataInputStream in = new DataInputStream(server.getInputStream());//exce
int input = 0;
//BufferedReader br = new BufferedReader(in);
byte byteArr[] = new byte[28];
try {
//read till the end of stream
//while((input = in.available()) != -1)
while ((input = in.read(byteArr)) != -1) {
System.out.println("Size read is " + input);
System.out.println("Data is " + bytesToHex(byteArr));
}
//System.out.println("inside finally");
server.close();//exce
//System.out.println("outside finally");
} catch (SocketTimeoutException ex) {
System.out.println("Socket timed out!");
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String args[]) throws IOException {
Serverc obj = new Serverc();
obj.connection();
}
}
這裏是我的控制檯
Waiting for client on port 9888...
Just connected to /106.208.71.50:61532
Size read is 28
Data is 55000016910001DB00FB63ABEEAFC1EC888F10263410050711148F3500000000
Size read is 28
Data is 55000016910001DB00FB63ABEEAFC1EC888F10263410050711148F3500000000
Size read is 28
Data is 55000016910001DB00FB63ABEEAFC1EC888F10263410050711148F3500000000
Size read is 28
Data is 55000016910001DB00FB63ABEEAFC1EC888F10263410050711148F3500000000
Size read is 28
Data is 55000016910001DB00FB63ABEEAFC1EC888F10263410050711148F3500000000
在這裏,我需要從輸出中刪除55000016。 先感謝
如果你的陣列是不是大規模的,只是把它轉換成一個列表,然後通過一個刪除一個元素(該列表將轉移陣列)的基礎上讀取標籤我的陣列有可能成爲massive.because在我的應用程序 –
大量的標籤需要被採納 – Miller