Thread: Need to read hex values out of a file

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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 << " ";
        }
    }

  2. #2
    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