Convert into hex

Hello,

I want to get content of a file and store it in a string(?), but also convert that string into hex number. I do it via this code, I'm only getting 6 hex characters where there should be a lot more. Is it because the hex number would be so big?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <fstream>
#include <string>
#include <ios>
#include <sstream>

using namespace std;

int main()
{
  string line;
  int i ;
  ifstream myfile ("someFile");
  if (myfile.is_open())
  {
    while ( myfile.good() )
    {
      getline (myfile,line);

      // Original file content;
      cout << line << endl;

      stringstream convert ( line );
      convert>> std::hex >> i;
      cout << hex << i << endl;
    }
    myfile.close();
  }

  else cout << "Unable to open file";


  return 0;
}
any value is stored as binary so this line:
convert>>std::hex>>i;
the std::hex doesn't do anything.

Now if you want more digits, try including <iomanip> and doing this:
cout << setw(8) << setfill('0') << hex << i << endl;
I'm only getting 6 hex characters


I'm getting a 4 byte value "cccccccc", which is the uninitialized integer i.

Is your file a text file (does it make sense in notepad)?

Don't know much about stringstreams, but I don't think you construct them with a string.
Last edited on
I did this:

cout << setw(8) << setfill('0') << hex << i << endl

But I'm still not getting the result.

I have this image which is like 20kb and I want the entire file in a one hex number. Maybe a hex string but then convert that into a number? Do I need to include something for large numbers?
Topic archived. No new replies allowed.