Hex Confusion....

Posts 1–6 of 6 · Page 1 of 1
Hex Confusion....
I am very confused... To put it lightly

First i Will post a code fragment
[php]
char bit = '\x42';
char mi = 0x42;
char f = '0x42';

cout << bit << endl << mi << endl << f << endl;
[/php]

The output to this is
Code:
B
B
2
My question is why....

'\x42' is B, that makes sense, the hexadecimal ascii character code for B is 0x42, and \x is the escape sequence for hexadecimal values, thus '\x42' == 0x42

0x42 is Appearently B also. This kinda makes sense, with everything i said before, but apparently even without the ' ' it is recognized as a character

'0x42' is 2...... This is where i am completely confused, why in the world is '0x42' the integer 2?

From what i know '\x42' and 0x42 should be equivalent, which it is. The last one just confounds me thought

Thanks for your time. I'm sure i'm overlooking something.

Edit: After playing with it some more, it would seem that it could just be randomly Organized Nonsense. I'd still like a definitive answer though.
Quote Originally Posted by Matrix_NEO006 View Post
maybe \ gets rid of 0.
The \xnnn is an escape sequence. It signifies the nnn part as a hexadecimal number. Escape sequences are meant to be used in Quotes, as a char, or in a string.

Pay close attention to the Inclusion of Quotes in my Post above :/
char f = '0x42';
ok what do we know about 'char'? Char is basically a signed byte.
What does a char hold? 1 byte or 1 character (8 bits)

Since there's no escape sequence here You're basically trying to
stick 4 'bytes' (32 bits): 0,x,4 and 2 into char f.
Since you only have room for 1 , you end up with the last 8 bits/last byte, namely '2'
Quote Originally Posted by B1ackAnge1 View Post
char f = '0x42';
ok what do we know about 'char'? Char is basically a signed byte.
What does a char hold? 1 byte or 1 character (8 bits)

Since there's no escape sequence here You're basically trying to
stick 4 'bytes' (32 bits): 0,x,4 and 2 into char f.
Since you only have room for 1 , you end up with the last 8 bits/last byte, namely '2'
Yay BlackAngel is here. Thank you that completely made sense =D. I wish i knew half as much as you did
Quote Originally Posted by zeco View Post
Yay BlackAngel is here. Thank you that completely made sense =D. I wish i knew half as much as you did
Same here. That was confusing as hell.
Posts 1–6 of 6 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Tags for this Thread

Talk with us