Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I Want the Hex, not the ASCII

Status
Not open for further replies.

sph147

Programmer
May 17, 2004
19
US
I'm reading in a *u_char that should be a bunch of hexidecimal flags and then outputing using cout<<. And low and behold, it wants to give me lots of ascii characters. How do I get the cout<< operator to stop converting to ascii and leave them how they are??.
 
In actuality, the code just stores em as bits and 13 == 0x0D == 1101 == 015 (dec,hex,bin,oct)

There should be a specifier for cout to output 3 of 4 of these formats

cout<<dec<<value<<endl;
cout<<hex<<value<<endl;
cout<<oct<<value<<endl;

for binary, you need to loop on each bit

Matt
 
Actually i got it working with a little of both solutions.

(in a for loop with control i)
cout<<hex<<(int)myvalue

Thanks for both, but now i have a new problem too.
I'm basically looking at packets and want to be able to tell that it is an IP protocol packet, so it should have the hex flag 0x0800 at a certain point. But since i'm working with a *u_char where i look at it as an array myvalue[x]. The question is, is each spot in that array one digit in decimal form? hex form? If anyone knows, that'd be great.
 
Since it is uchar but you are looking for either a short or int/long value, you will not be able to do it unless you know where to look.

uchar = 8 bits
short = 16 bits
int/long = 32 bits (MSVC sizes) I believe .net has a long as a 64 bit value

The value you are looking for 0x0800 is not in the uchar range as it is looking at bit 11 (in a zero based bit count)

100000000000 (binary)

if, instead you had an int or short pointer and looked at the data, you could compare it as that type

To determine if the bit is set, it is simply a bitwise and

if(value & 0x0800)
{
// bit is set
}

Matt
 
Thanks again, I think i can see where you're coming from. What I did was a little different. I'm not sure how the *u_char is storing them, but when I

cout<<dec<<u_char<<" "

in a for loop so i can see each individual integer stored, i get numbers between 0 and 255 (0xff) which makes sense cause i'm looking at a paket. What i did then was figure out where somewhere in the sequence I continually saw
"..X 8 0 Y....." and that is what i was looking for. So i figured out that was the u_char[12] and u_char[13] and can compare now using

if ( ((int)u_char[12] == 8) && ((int)u_char[13] == 0))

then it has the flag 0x0800. I highly doubt this is anywhere near efficient though, so any better suggestions would be great!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top