How to read hexadecimal code from text file???

Oct 1, 2013 at 3:04pm
closed account (3hMz8vqX)
Hi all,
How to read hexadecimal code from text file???
for example:
my text file contains hexadecimal number now i need to read
it and check if it is 5E or something ...
Hope you understand...
Thankyou everyone in advance!!! :)
Oct 1, 2013 at 4:32pm
What part are you confused about? There's lots of things you could mean and I'm not sure which it is.
Oct 1, 2013 at 6:26pm
closed account (3hMz8vqX)
Okay,
this is the thing...
my text file contains hexadecimal digits...
0x25
0x3A
0x5E
etc...
now in the program...
int val=0x5E
now i need to read the text file
and check if int val matches with any of the values in the text file...
I need to read the hex digits as hex digits from the text file...

Please help me:)
Thankyou everyone in advance!!! :)
Oct 1, 2013 at 6:44pm
Look at the example at the bottom of this page:
http://www.cplusplus.com/reference/istream/istream/operator%3E%3E/
Line 12, specifically. You can do that same trick with a file.
Oct 1, 2013 at 8:27pm
closed account (3hMz8vqX)
Thankyou L B for your reply...:)
okay one more doubt...
in the text file can i have
5E
3A
etc...
and val=3A
instead of:
0x3A
0x5E
and val=0x3A
Please give me a suggestion
and thats it ! :)
Thankyou everyone in advance!!! :)
Oct 1, 2013 at 8:34pm
For your actual code, there is no alternative - the C++ standard says that only literals beginning with "0x" can be considered hexadecimal. For the text file, though, you could just read the numbers as strings first, prepend "0x", and then use a stringstream to extract the hexadecimal number.
Oct 1, 2013 at 8:58pm
closed account (3hMz8vqX)
Thankyou very much L B:)
Im very happy:)
Regards,
Aravind
Oct 1, 2013 at 9:23pm
or the text file, though, you could just read the numbers as strings first, prepend "0x", and then use a stringstream to extract the hexadecimal number.

There's no need to add the 0x prefix when reading un-prefixed hex numbers; you just need to use std::hex to set hexadecimal mode first.

Andy
Last edited on Oct 2, 2013 at 9:08am
Oct 2, 2013 at 5:46am
closed account (3hMz8vqX)
Okay,
so int val=3A
is that correct?
Thankyou everyone in advance!!! :)

Regards,
Aravind.
Last edited on Oct 2, 2013 at 5:52am
Oct 2, 2013 at 9:07am
so int val=3A
is that correct?

No, in code you must use the 0x prefix

I was responding (unclearly, sorry) to L B's suggestion to add the prefix when reading from values from file.

Andy

PS Edited prev post to make it (hopefully) a bit clearer.
Last edited on Oct 2, 2013 at 9:08am
Oct 2, 2013 at 10:46am
closed account (3hMz8vqX)
Okay,
no problem! Thankyou very much, Andy ...!
and thankyou very much L B...!
Regards,
Aravind.
Topic archived. No new replies allowed.