Wierd issue using cout to print hex

I have these two lines of code:

1
2
printf("Start Delimiter:\t\t\t %x\n", dataBuff->start_delim);
cout << "Start Delimiter:\t\t\t" << std::hex << dataBuff->start_delim << endl;


The first one will print the hex value for the start_delim member no problem:
Start Delimiter: 7E

The second line always prints the ~ chracter no matter what I do..

Can anyone tell me why?

The cout lines in my code print the other values in hex no problem...
Just 7E its having a problem with...

Thanks
Last edited on
Try std::cout.setf(std::ios_base::hex, std::ios::basefield);
That worked! but why? what is the bid deal with the 7E?

I tried this before I got your reply:
 
cout.flags ( ios::right | ios::hex | ios::showbase );


it didnt effect the 7e but did all others...
I dont understand the difference.

How do I unset the code you gave me so the rest of the fields print normally?

THanks man!!!!!

That worked! but why?

*shrug* I don't have a clue. I just posted that on the off-chance it would work, because it's always worked for me that way. Tbh I don't use the iostream library very often; I tend to program more in C, so I don't know it that well.

How do I unset the code you gave me so the rest of the fields print normally?

Store the return value of setf, and then use unsetf on it:
1
2
3
std::ios_base::fmtflags ff = std::cout.setf(std::ios_base::hex, std::ios::basefield);
// ...
std::cout.unsetf(ff);
ooops! I spoke too soon...
When I inserted the line of code you gave me I accidentally left in the printf statement and that is why I thought it worked... but it is not...

I dont freakin get this.... why in the world would one hex character be ignored?

thanks for your help...

That is indeed strange. It prints a tilde ('~') whenever you try to print 0x7E? That is odd, because 0x7E is the ASCII code for tilde. Are you using characters (chars) to store the data?
If I cast it to an INT with your line of code then it works...
since this is a one byte field that I am working with I delcared it as char... so i guess the setf that you suggested must be expecting an int type?





yes, I am using char is there a better way to store a single byte?
I would assume so. Unfortunately operator<< applied to std::cout assumes a char is referring to a character in the ASCII table. I guess you'll either have to store it in an int or typecast it as such. I'd also recommend you don't assume a char is one byte. It might not be on all platforms.
Last edited on
Gotcha.

Can you reccomend a way to store a single byte, is there a standard for this? Should I use bit fields in my struct instead?


Just cast it to an integer before you output it. By the way, a char IS one byte (in C++) on all platforms. sizeof(char) == 1 always.
You can't really do it with char, and even then, you can't assume char is a single byte.You may as well just store it as an integer. 0x7E == 0x0000007E, but the latter is 4 bytes and not 1.

Edit: didn't press submit.

@firedraco,
I didn't know that.

Also, (1337 + 1000)th post!
Last edited on
thanks guys... I think I am good on this one...
Ok. No problem.
Topic archived. No new replies allowed.