11

As the title says, how do you read hex values using fstream?

i have this code: (let's say we have "FF" in the file.)

fstream infile;
infile.open(filename, fstream::in|fstream::out|fstream::app);

int a;
infile >> std::hex;
infile >> a;
cout << hex << a;

but this does not give me any output instead of ff. I know there is a fscanf(fp, "%x", val) but I am curious is there any way to do this using stream library.

UPDATE:

My code was right all along, it turns out my error was I couldn't read "FFF" and put it in variable a,b,c like this

while (infile >> hex >> a >> b >> c)
  {
    cout << hex << a << b << c << "\n";
  }

Can somebody help me with this? do I have to separate every HEX values i want to read with space? because infile >> hex >> setw(1) doesn't work..

6
  • 1
    This has nothing to do with hex values. Your error lies elsewhere. Commented Feb 18, 2011 at 11:39
  • before asking to here, search on google. I am sure you will find answer for this trivial question Commented Feb 18, 2011 at 11:46
  • @gcc I searched on google but it didn't work for my case.. Commented Feb 18, 2011 at 11:49
  • @etarion you were right, but I have another question please help me :| Commented Feb 18, 2011 at 11:50
  • 4
    @gcc: Sometimes it's difficult to express your query in that "Text box". Therefore, it's desirable to ask it in here. Also, you may end up getting broken links or incorrect information. This site is backed up by active community. So an answer in here is worth more even if the same could have been found using Google. Commented Feb 18, 2011 at 11:51

4 Answers 4

12

You can use the hex modifier

int n;
cin >> hex >> n;
Sign up to request clarification or add additional context in comments.

1 Comment

std::hex and hex are the same :)
7

This works:

int main()
{
    const char *filename = "blah.txt";
    ifstream infile(filename, fstream::in);

    unsigned int a;
    infile >> hex >> a;
    cout << hex << a;
}

1 Comment

@gcc: Unnecessary; the destructor will do this implicitly.
6

You have to chain std::hex when reading, the same way you chain it for writing :

infile >> std::hex >> a;

3 Comments

thanks for the answer! I have tried that but also it does not work :(
@ardiyu: It does work. ideone.com/qs2Z5 Also using std::hex on a seperate line, like you did, works. Your error lies elsewhere.
@etarion @Nekresh You guys right it worked! but I got another question please see the update and help me :|
-2

Also make sure that your input file is written using a Hex editor and not a regular text editor. Otherwise a file foo.txt containing a character 'a' will be read as 0x61 and printed as 0x61 instead of 0xa. A nice Hex editor for linux is "Bless".

2 Comments

While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
My point is to use a Hex editor - doesn't matter which one. Therefore, the link is optional in my answer. It is only a suggestion. I understand your point though. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.