View Full Version : C++ cout Hex values
patryk77
February 25th, 2009, 06:06 AM
Hey,
I am writing a parser for binary data files, and would like to print the Hex values of each char.
I have a pointer, and I assign what it points to to an int.
int hexVal = *ptrStart;
cout.flags(ios::hex);
cout << hexVal << endl;
I tried it with two values, and while I expect A1 and 80, it prints ffffffa1 and ffffff80.
Could anyone tell me why it does that? And how to make it assign and print the correct values?
Thanks
Edit: I guess it must be during the conversion to int that it happens, right?
monkeyking
February 25th, 2009, 06:24 AM
I am in no way an expert but I think you are expecting the hexval of the first byte (8bits),
but cout gives you all 4 bytes (32 bits)(1 int).
you should use chars if you are really only interested in 1 byte.
Futhermore, you should consider using unsigned values, if you are planning to do bit stuff. otherwise your number are in 2-complements
good luck
patryk77
February 25th, 2009, 06:29 AM
Thanks for the reply. I am looking for a single byte value, but I am not sure how to get it.
I actually just tried a short int now, which gives me FFA1. Close but no cigar.
Edit: Meh, no big deal. Will compare with 0xFFA1 for now.
I would still like some input if anyone knows the right way to achieve this, but I need to have it done quick for now.
eye208
February 25th, 2009, 07:21 AM
I would still like some input if anyone knows the right way to achieve this, but I need to have it done quick for now.
cout << (hexVal & 0xFF) << endl;
the_unforgiven
February 25th, 2009, 07:31 AM
You use setf() (http://www.cplusplus.com/reference/iostream/ios_base/setf.html) to set flags and not flags().
movieman
February 25th, 2009, 07:35 AM
you should use chars if you are really only interested in 1 byte.
If you use chars you're likely to get a result you didn't expect :).
Unsigned int and/or (int & 0xff) is the way to go.
patryk77
February 25th, 2009, 07:55 AM
You use setf() (http://www.cplusplus.com/reference/iostream/ios_base/setf.html) to set flags and not flags().
http://www.cplusplus.com/reference/iostream/ios_base/flags.html
flags() works too. Is there a reason for the preference?
Edit 2: Should have read the page first. flags() unsets the other ones. where as setf() just changes specific ones
If you use chars you're likely to get a result you didn't expect :).
Unsigned int and/or (int & 0xff) is the way to go.
Yeah hehe, tried the char method... Printed out garbage.
I'm using unsigned shorts now. Should be good too, right? Edit: they give me the results I expect, even if 0xFFA1 instead of just 0xA1, but I can live with that.
Thanks for the replies :)
monkeyking
February 25th, 2009, 08:31 AM
If you use chars you're likely to get a result you didn't expect :).
Unsigned int and/or (int & 0xff) is the way to go.
If you only need 1byte, then unsigned char is the only choice.
Why should it give strange results?
soltanis
February 25th, 2009, 08:35 AM
int hexVal = *ptrStart;
cout.flags(ios::hex);
cout << hexVal << endl;
Yes. You are using an integer to store the value of a character. Since an integer seems to be 4 bytes on your system, the extra 3 bytes are being printed. Use an unsigned char instead.
patryk77
February 25th, 2009, 08:41 AM
Unsigned char also prints garbage.
Will stick to unsigned short, I guess.
the_unforgiven
February 25th, 2009, 08:51 AM
This following code seems to be working for me:
#include <iostream>
int main()
{
int hexVal = 0xa1;
std::cout.setf(std::ios::hex, std::ios::basefield);
std::cout.setf(std::ios::showbase);
std::cout << hexVal << std::endl;
return 0;
}
Note that I'm using a plain int and it still prints '0xa1' for me.
eye208
February 25th, 2009, 09:33 AM
Note that I'm using a plain int and it still prints '0xa1' for me.
Chars are signed by default. If you convert char 0xA1 to signed int, the result is not 0x000000A1 (161) but 0xFFFFFFA1 (-95).
geirha
February 25th, 2009, 10:41 AM
int hexVal = *ptrStart;
cout.flags(ios::hex);
cout << hexVal << endl;
If you give cout an unsigned char, it will print it as a char. But if you give it an int, it will print it as a number.
unsigned char hexVal = *ptrStart;
cout << showbase << hex << (int)hexVal << endl;
patryk77
February 25th, 2009, 03:55 PM
If you give cout an unsigned char, it will print it as a char. But if you give it an int, it will print it as a number.
unsigned char hexVal = *ptrStart;
cout << showbase << hex << (int)hexVal << endl;
Awesome! Thanks :)
This works, and comparing it to 0xA1 works :)
Powered by vBulletin® Version 4.2.2 Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.