Sign in to follow this  

C++ Read Hex Data from a file

This topic is 3593 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello all, I have a file which contains the following: 26 25 2F 2D ... That is, all HEX data - interpreted by us as --> & % / - I need to write a C++ code, which will open the file in read mode, and take the input as HEX itself, and not string. That is, the C++ program will interpret the data as HEX digits. I tried, but the program is reading as string. Any help will be welcome.

Share this post


Link to post
Share on other sites
If you have that hex as characters in the file then your program is indeed correct in reading it out as a string. Those characters, although can be read has hex, are in fact representing completely different data.

Share this post


Link to post
Share on other sites
Quote:
Original post by ambarish_mitra
That is, the C++ program will interpret the data as HEX digits.


Did you try using the standard library?

#include <istream>
#include <iomanip>

using namespace std;


int readHex(istream& istr)
{
int value;
istr >> hex >> value;
return value;
}

Share this post


Link to post
Share on other sites
Quote:
Original post by ambarish_mitra
Hello all,

I have a file which contains the following:
26 25 2F 2D ...


That is, all HEX data - interpreted by us as --> & % / -

I need to write a C++ code, which will open the file in read mode, and take the input as HEX itself, and not string.

That is, the C++ program will interpret the data as HEX digits.

I tried, but the program is reading as string.

Any help will be welcome.


I think you are confused about how data works.

The file contains bytes. Each byte is (and I am making a huge simplification here) a number from 0 to 255, inclusive. We can display this number in many ways. One is to draw a symbol, chosen from a set of 256 symbols, that corresponds to that number. That is, we look it up in the ASCII table, and turn the number 38 into &, 37 into %, 47 into / and 45 into -. Another is to use the symbols 0 through 9 and A through F, and use two such symbols to express the number in human-readable, hexadecimal format. But each of those symbols, in turn, corresponds to a byte.

What is actually in the file? There is no such thing as "HEX data". There can be a file 4 bytes long, which you are viewing with a hex editor (causing it to show "26 25 2F 2D" on screen), or a file with the 4 bytes, which you view with a text editor (such as notepad, causing it to show "&%/-"; or a file with 11 bytes, which you view with a text editor and see "26 25 2F 2D" (i.e. a hex representation of some data), or a file with 11 bytes, which you view with a hex editor and see ... well, see if you can figure that out for yourself. :) (The space character has numeric value 32; digits are 48 through 57; capital letters are 65 through 90.)

Now, given what's in the file, what do you want to end up in memory?

Share this post


Link to post
Share on other sites

This topic is 3593 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this