in Education by
Am working on decoding a protocol used by a certain gps device to communicate. Right now, am analyzing the first data packet that it sends. I am able to read it, but am thinking am not reading it properly. Here is what i got so far: public static String toHex(byte[] bytes) { BigInteger bi = new BigInteger(1, bytes); return String.format("%0" + (bytes.length << 1) + "X", bi); } private void ProcessInitialPacket(){ int port = 1954; System.out.println("Listening on port :"+port); byte[] data = new byte[17]; byte[] ackPacket = new byte[2]; byte[] dataPacket= new byte[15]; try { ServerSocket sSocket = new ServerSocket(port); Socket cSocket = sSocket.accept(); DataInputStream dataIN = new DataInputStream(cSocket.getInputStream()); int packetSize=dataIN.read(data,0,data.length); System.arraycopy(data, 0, ackPacket, 0, 2); System.arraycopy(data,2,dataPacket,0,15); System.out.println("Total packet size: "+ packetSize); System.out.println("ACK PACKET : "+ toHex(ackPacket)); System.out.println("DATA PACKET: "+ toHex(dataPacket)); System.out.println("FULL PACKET: "+ toHex(data)); } catch (IOException e) { e.printStackTrace(); } } the output: -PARSER()-- -INITSESSION-- Listening on port :1954 Total packet size: 17 ACK PACKET : 000F DATA PACKET: 333532383438303236323631393534 FULL PACKET: 000F333532383438303236323631393534 ------CLOSESESSION------------ Now, my problem: what is happening here is that the device sends a [0x00][0x0F]xxxxxxxxxxxxxxx where the xxxxxxx is its imei (the data packet). My problem is that there are way too many 3´s on the data packet, so the real valid output is 352848026261954 which you obtain by removing 3´s. my question is: can this behavior come from my code or its part of the protocol? i can correct this programmatically but am wanting to know it there is a way that code can cause these extra 3s. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
You are looking at the hex of the ascii values which needs to be decoded as numbers. The character '0' as a decimal is 48 or as hex 0x30, up to '9' is 57 as a decimal or 0x39 as hex. So a sequence of bytes 33 35 32 38 34 38 30 32 36 32 36 31 39 35 34 is "352848026261954" as ASCII characters. I would change your code like this dataIN.readFully(data); // always read 17 bytes, not less String text = new String(data, 0, 2, 13); // decodes 8-bit bytes as a String long number = Long.parseLong(text);

Related questions

0 votes
    Which of these is a protocol for breaking and sending packets to an address across a network? (a) ... programming questions and answers pdf, java interview questions for beginners...
asked Oct 25, 2021 in Education by JackTerrance
0 votes
    It can be a software program or a hardware device that filters all data packets coming through the internet, a ... . it is known as the_______: Antivirus Firewall Cookies Malware...
asked Mar 4, 2021 in Technology by JackTerrance
0 votes
    Which of these methods of httpd class is used to read data from the stream? (a) getDta() (b) ... java programming questions and answers pdf, java interview questions for beginners...
asked Oct 25, 2021 in Education by JackTerrance
0 votes
    Which of these is used to read a string from the input stream? (a) get() (b) getLine() (c) read() (d) ... chapter I/O & Applets of Java Select the correct answer from above options...
asked Feb 24, 2022 in Education by JackTerrance
0 votes
    Which of these is used to read a string from the input stream? (a) get() (b) getLine() (c ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 24, 2021 in Education by JackTerrance
0 votes
    Which of the protocol is not used in the network layer of the TCP/IP model? (a) ICMP (b) IP (c) IGMP ... bank, Cyber Security questions and answers pdf, mcq on Cyber Security pdf,...
asked Nov 3, 2021 in Education by JackTerrance
+1 vote
    Microsoft uses industry standard _______________ dynamic routing protocol to exchange routes between your on-premises ... instances in Azure, and Microsoft public addresses....
asked Oct 20, 2020 in Technology by JackTerrance
0 votes
    Which of these is a type of stream in Java? (a) Integer stream (b) Short stream (c) Byte stream (d) Long ... I/O & Applets of Java Select the correct answer from above options...
asked Feb 24, 2022 in Education by JackTerrance
0 votes
    Which of these is a type of stream in Java? (a) Integer stream (b) Short stream (c) Byte stream ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 24, 2021 in Education by JackTerrance
0 votes
    What is the purpose of sorted method of stream in Java 8?...
asked Nov 8, 2020 in Education by Editorial Staff
0 votes
    What is the purpose of limit method of stream in Java 8?...
asked Nov 8, 2020 in Education by Editorial Staff
0 votes
    What is the purpose of filter method of stream in Java 8?...
asked Nov 8, 2020 in Education by Editorial Staff
0 votes
    What is the purpose of map method of stream in Java 8?...
asked Nov 8, 2020 in Education by Editorial Staff
0 votes
    What is the purpose of forEach method of stream in Java 8?...
asked Nov 8, 2020 in Education by Editorial Staff
0 votes
    What is the difference between Collections and Stream in in Java 8?...
asked Nov 8, 2020 in Education by Editorial Staff
...