我有一个名为byteArr[]的字节数组,我需要从它移除前4个字节。我的代码如下所示。 在这里,我使用字节数组来存储输入字符串。我得到了一些不需要的字节与输出即前四个字节是不需要从第五起是正确的。 我的程序是使用rfid机器从尊重的rfid标签中获取id。
public class Serverc { final protected static char[] hexArray = "0123456789ABCDEF".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。 提前谢谢
I have a byte array named byteArr[].I need to remove first 4 bytes from it.My code is shown below. Here I use the byte array to store the input string.I got some unwanted byte with the output i.e the first four bytes is not needed from the fifth onwards is correct. My program is to take id from respected rfid tag using an rfid machine.
public class Serverc { final protected static char[] hexArray = "0123456789ABCDEF".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(); } }Here is my console
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 55000016910001DB00FB63ABEEAFC1EC888F10263410050711148F3500000000Here I need to remove 55000016 from the output. Advance thanks
最满意答案
如果你想跳过前四个字节改变这一点,
for (int j = 0; j < bytes.length; j++) {到类似的东西
for (int j = 4; j < bytes.length; j++) {或者你可以使用String.substring(int)
bytesToHex(byteArr).substring(8); // <-- skip the first 4 bytesIf you want to skip the first four bytes change this,
for (int j = 0; j < bytes.length; j++) {to something like
for (int j = 4; j < bytes.length; j++) {Or you might use String.substring(int)
bytesToHex(byteArr).substring(8); // <-- skip the first 4 bytes如何从Java中的字节数组中删除前4个字节?(How to remove first 4 bytes from a byte array in Java?)我有一个名为byteArr[]的字节数组,我需要从它移除前4个字节。我的代码如下所示。 在这里,我使用字节数组来存储输入字符串。我得到了一些不需要的字节与输出即前四个字节是不需要从第五起是正确的。 我的程序是使用rfid机器从尊重的rfid标签中获取id。
public class Serverc { final protected static char[] hexArray = "0123456789ABCDEF".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。 提前谢谢
I have a byte array named byteArr[].I need to remove first 4 bytes from it.My code is shown below. Here I use the byte array to store the input string.I got some unwanted byte with the output i.e the first four bytes is not needed from the fifth onwards is correct. My program is to take id from respected rfid tag using an rfid machine.
public class Serverc { final protected static char[] hexArray = "0123456789ABCDEF".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(); } }Here is my console
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 55000016910001DB00FB63ABEEAFC1EC888F10263410050711148F3500000000Here I need to remove 55000016 from the output. Advance thanks
最满意答案
如果你想跳过前四个字节改变这一点,
for (int j = 0; j < bytes.length; j++) {到类似的东西
for (int j = 4; j < bytes.length; j++) {或者你可以使用String.substring(int)
bytesToHex(byteArr).substring(8); // <-- skip the first 4 bytesIf you want to skip the first four bytes change this,
for (int j = 0; j < bytes.length; j++) {to something like
for (int j = 4; j < bytes.length; j++) {Or you might use String.substring(int)
bytesToHex(byteArr).substring(8); // <-- skip the first 4 bytes
发布评论