Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Hex to Integer Problem

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Hex to Integer Problem

    I am currently trying to create a program that will extract information from an 'index.dat' file used by Internet Explorer to store internet history.
    Currently the problem arises when attempting to find the file offsets for various HASH tables within the file (these tables are where the history entries are stored).
    The hex values containing these offsets are at known locations within the file, so finding them is not a problem, however when attempting to extract these values my program seems to collapse. After much 'googling' and messing around I think I've identified where the problem lies.
    When looking at the file in a hex editor I can see that the hex values of 0x01 and 0x00 are both given the symbol of '.', ultimately meaning my program treats the 0x01 values as 0x00 messing up any calculation I attempt to carry out.
    Below is an extract of code to show my calculations to extract the values:

    int hashOffset = hexToInt(raf, STARTING_OFFSET, NUMBER_BYTES);
    System.out.println("HASH Table Offset: 0x" + Integer.toString(hashOffset,16));
     
    //size of HASH table
    int hashSize = hexToInt(raf, hashOffset + 4, NUMBER_BYTES) * RECORD_SIZE;
     
    System.out.println("HASH size: " + hashSize + " Bytes");
     
    //next HASH table offset
    int nextHashOffset = hexToInt(raf, hashOffset + 8, NUMBER_BYTES);
    System.out.println("nextHashOff: 0x" + Integer.toString(nextHashOffset,16));
    System.out.println("");
    private int hexToInt(RandomAccessFile raf, int startLocation, int numberOfBytes) throws Exception
    {
    		byte[] buf = hexRead(raf, startLocation, numberOfBytes);
                    int total=0;
     
                    for(int i = 0; i < numberOfBytes; i++)
                    {
                        total += buf[i] << 8*i;
                    }
    		return total;
    }//end hexToInt
    private byte[] hexRead (RandomAccessFile raf, int offset, int numberOfBytes) throws Exception
    {
    		byte[] buf = new byte[numberOfBytes];
    		raf.seek(offset);
    		raf.read(buf,0,numberOfBytes);
    		return buf;
    }//end hexRead

    I dont't believe the problem lies in my calculations as they work perfectly for previous values. None of these values however contain 0x01. Possibly lies in the way the file is read?
    If anyone could offer any advice as how to get round this, it would be greatly appreciated.

    Thanks in advance,
    David.


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Hex to Integer Problem

    I'm not sure as a Random Access File opens file sin binary mode, which is very much required for this. If you open a file in binary mode you will distinguish between 0x00 and 0x01.

    Regards,
    Chris

Similar Threads

  1. How to fetch integer data from excel
    By nehakuls in forum JDBC & Databases
    Replies: 5
    Last Post: May 14th, 2010, 01:44 AM
  2. int and Integer
    By chronoz13 in forum Java Theory & Questions
    Replies: 5
    Last Post: December 31st, 2009, 03:20 AM
  3. check if a number is an integer
    By rsala004 in forum Java Theory & Questions
    Replies: 3
    Last Post: August 15th, 2009, 03:51 PM
  4. [SOLVED] How to make a integer negative if it meets a certain criteria?
    By Lizard in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 14th, 2009, 02:27 PM
  5. How to check that console input should be integer only?
    By Konnor in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: February 2nd, 2009, 05:37 AM