Thread: Reading in hex values from a specific segment and offset.

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    100

    Reading in hex values from a specific segment and offset.

    Hi all,

    If I wanted to read in the hex values found at a specific point in a binary file, how would i go about doing this? Would it need to be converted to decimal or binary? This is what ive got so far

    Code:
    	char beh[11];
    	ifstream updatedat("c:/sol.exe", ios::hex | ios::in);
    	updatedat.seekg(10,ios::end);
    	updatedat.read(beh,10);
    	MessageBox(NULL,beh,"here it is", MB_OK);
    	updatedat.close();
    Im trying to read in 10 hex values and store them in the char array beh. I set seekg to 10 as this is the position that i want to start reading from.

    When the MessageBox pops up it shows strange characters where as my hex editor shows it in hex (obviously).

    Is the code correct but the problem being the hex stored in the char array? If so then how should i store it to show the actual hex values rather than symbols?

    Thanks

    Thanks
    Last edited by cloudy; 01-23-2006 at 05:11 PM.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    the values in the file are NOT hex -- they are binary. you have to convert them to the desired displayable format. Something like below.

    There is no such flag as ios::hex in the open statement -- look up open() and you'll see that it should be ios::binary.
    Code:
    	char beh[11];
            char text[40] = {0};
            char  tmp[8];
    	ifstream updatedat("c:/sol.exe", ios::binary | ios::in);
    	updatedat.seekg(10,ios::end);
    	updatedat.read(beh,10);
            for(int i = 0; i < 10; i++)
            {
                sprintf(tmp,"%X ",beh[i]);
               strcat(text,tmp);
           }
    	MessageBox(NULL,text,"here it is", MB_OK);
    	updatedat.close();

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    100
    Hi there,

    Thanks a lot for helping

    I completely understand your code but it doesnt appear to work, the line:

    Code:
    strcat(text,tmp);
    is causing the program to crash i believe. If ii leave that line in and run the program it immediately terminates, however if i comment out that line then the program doesnt terminate.

    Also why is ther size of the char array for text 40 and the temp 8? Im guessing that the 8 is to do with the size of a byte.

    Thanks again for your help

    Mike

    Oh btw, my include files are:

    Code:
    #include <fstream>
    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    using namespace std;

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    The problem is buffer overflow. Change the sprintf() line like below
    Code:
                sprintf(tmp,"%X ",(unsigned char)beh[i]);
    The size of the array is kind-of arbitrary, and have nothing to do with size of byte. Its always better to have buffers too big than too little.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Relocation in .obj -files
    By willkoh in forum C++ Programming
    Replies: 6
    Last Post: 04-06-2005, 01:59 PM