Thread: Need to read hex values out of a file

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    2

    Need to read hex values out of a file

    i am making a program that extracts certain hex values out of a file and saves in another file.

    but i have a problem, i don't know how to read hex values out of a file. i know how to read a regular text file but not hexidecimal. i need to place the file content in a variable and then separate the variable by offsets.

    thanks in advance for any help
    sorry if i sound stupid

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Try something like this
    Code:
    #include <string>
    #include <iostream>
    #include <iomanip>
    #include <sstream>
    
    using namespace std;
    
    int main() {
        char buffer [] ="0x1234 0x345";
        stringstream sstr( buffer );
        int word;
        sstr >> hex;
        while( sstr >> word ) {
            cout << word << " ";
        }
    }
    You could use a filestream instead of the stringstream
    Kurt

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    2
    i really don't understand how that would work, where do i load the file? i just need to store the hex in a var from a file

  4. #4
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    #include <string>
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    
    using namespace std;
    
    int main() {
        ifstream fstr( "somefile.txt" );
        int word;
        fstr >> hex;
        while( fstr >> word ) {
            cout << word << " ";
        }
    }

  5. #5
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    ...i just need to store the hex in a var from a file
    Well, here's the thing.... You can't store a variable in hex! It's just a number.

    You can input a number in hex format, or display a number in hex, but in the computer's memory it's "just a number" (stored in binary, of course).

    Depending on what you're doing, you might find a Hex Editor useful. A hex editor allows you to view any file in hex, and edit the bytes. I use a hex editor called XVI32 (FREE !!!!).

    Most hex editors will also show the ASCII characters for any bytes that fall in the "ASCII range". Of course, the hex editor actually has no idea if a value of 65 (41 hex) represents the value 65 or the character 'A'.

    ...i know how to read a regular text file but not hexidecimal.
    Open your file in binary mode. In binary node, you can read-in the file as numbers. The term binary is a little misleading.... I consider it the "raw mode"

    Although the text mode is the default, it's a special case. Most file I/O is done in binary mode.
    Last edited by DougDbug; 05-05-2006 at 04:07 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading in a text file containing hex values
    By gaza2k1 in forum C Programming
    Replies: 34
    Last Post: 02-29-2008, 07:15 PM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM