13 Replies Latest reply: Mar 27, 2008 10:51 AM by 807591 RSS

    Reading hex files

      Hi, im having trouble reading a hex file in java. I want my program to search through the file and look for IP and ARP packets and then print out there values and their correpsonding bytes (ip, headerlen, id, checksum, etc) I have some code for it but its not quite working. Could some one please give me some tips on how to improve my code. Cheers:
      private void getPacketData() throws IOException
                System.out.println("Packet Data information: ");
                String line = "";
                String[] values = null;
                InputStream src = new FileInputStream("C:/Users/Andrew/workspace/tcpdump_text_file");
                BufferedReader reader = new BufferedReader(new InputStreamReader(src));
                while(src.read() != -1)
                     for(int i = 0; i < 20; i++) {
                          line = reader.readLine();
                          values = line.split("");
                          if(values.length > 5 && values[4].matches("0800"))
                               System.out.println("IP packet"); showbytes(2, src);
                          else if(values.length > 5 && values[4].matches("0806"))
                               System.out.println("ARP packet"); showbytes(2, src);
                          else {
                               showbytes(2, src);
        • 1. Re: Reading hex files
          You've not detailed what about your code you feel is "not quite working."

          I assume all the magic in your program is performed in showbytes(). It seems odd to me that you've placed one line of the source file into your variable 'line', yet pass the whole InputStream 'src' to showbytes(). Guess I've seen stranger things, but I wonder how that method knows what line of the InputStream it needs to process.

          Not knowing the file format, or Java very well, I could be pretty far off base.
          • 2. Re: Reading hex files
            So src and reader are initialized to the same file...but for every byte you read of src, you read 20 lines of reader? Is src supposed to be your header? Or is reader your header? I can't figure out what you're trying to do.
            • 3. Re: Reading hex files
              Well what i mean is when i run my program it should read thru the hex file and search for the values 0800 and 0806 and print out IP packet and ARP packet, but it does not work, it seems to miss the first two clauses of the if statement and then jump onto the "else" condition. i had the file source in another method called mainfn() to run the whole program but didnt work, so decided to move into the method above to see if that worked but it didnt. showbytes() is where all the magic happens. Any ideas?
              • 4. Re: Reading hex files
                I'd throw some println()s in there to see what it is actually splitting from the read line. Starting with something like values.length and values[4] may show the problem.

                Why use the RE .matches() instead of just .equals() for your checking, please?
                • 5. Re: Reading hex files
                  The regex in your split() is empty. Is there supposed to be a space there? With an empty regex wouldn't that make put the entire line into values[0]? And therefore never have values.length > 5?
                  • 6. Re: Reading hex files
                    well i assumed that in a hex file, the values are spaced like 00 00 00 08 00. All im wanting the program to do is run theough the file and look out for types of packets such as 0800 and 0806 and retrieve the types and also their packet data. But at the moment im trying to get the program to work for reading and picking out the packet types when the program runs throught the file. Ive tried various attempts at his but they are not successfull. Is there anything in the code that i shoudl change/add to make this work?
                    • 7. Re: Reading hex files
                      If there's a space, make it
                      values = line.split(" "); // with a space
                      instead of
                      values = line.split(""); //no space 
                      • 8. Re: Reading hex files
                        ok, thanks. Just tried that but still doesnt seem to work when i run the program, just displays the whole row of hex values. I could post all of my java class if you wanted to have a look at it?
                        • 9. Re: Reading hex files
                          That's not necessary. If it's not going into your If's, then you have a problem with your split, or with how you think the file looks. Use println's after your split() and before your If, to see what's in values.
                          System.out.println("Length: " + values.length);
                          for (String s : values)
                              System.out.println(s + " ");
                          • 10. Re: Reading hex files
                            well i assumed that in a hex file, the values are spaced like 00 00 00 08 00.
                            Wrong assumption. You're confusing the content of a hex file and its textual representation in a hex editor.viewer.

                            I suggest you open a smallish hex file in Notepad or any straight text editor and take a look at the contents.

                            Also, I think (note: think) that using a BufferedReader isn't the best way to read the content of a hex file, according to the documentation it's meant for a character input stream.

                            • 11. Re: Reading hex files
                              just tried that and it prints out the tcpdump file and in between prints out 0100 Length: 1 & 0415 Length: 4 & 4115 Length: 5 and so on..... So does that mean i have a problem with my split() then?
                              • 12. Re: Reading hex files
                                "Hex file" is a question-begging phrase. Is this file actually a "hex dump", which contains ASCII characters 0-F representing hexadecimal values? That's a LOT more common in my experience.

                                In other words, when you open it in notepad or other text-only editor, you see lines of hex values like "00801aef2b" rather than a host of randomish characters?

                                If so, you're reading and interpreting a text file, which is different than if it's the binary byte values in there.
                                • 13. Re: Reading hex files
                                  ahh ok. when i open it in notepad, it displays all the random characters. the thing is ive been viewing the file in a hex editor. so basically ive been reading the file wrong in my program.