开发者

how to translate char into hex and binary

I have th开发者_如何学Ce following example in C:

int x = 123; 

size of int = 4 bytes.
hex value = 7b
binary value = 0111 1011

What will be the hex/binary value if my x was a char?

char x = 123;
size of char = 1 byte.
hex value = ??
binary value = ??


Since char is smaller than int, it would be the same. Except for leading zeros you would find in an int.

Try this:

  6   int x = 123;
  7 
  8   printf ("Size of int is  %d bytes\n", sizeof(int));
  9   printf ("Size of char is %d byte\n", sizeof(char));
 10 
 11   printf ("Hex value is %x\n", x);
 12   printf ("Hex value is %x\n", (char)x);
 13 
 14   printf ("As for binary value .... google is your friend\n");

Output is:

Size of int is  4 bytes
Size of char is 1 byte

Hex value is 7b
Hex value is 7b

As for binary value .... google is your friend This site seems to explain things well


Its hex and binary values will be exactly the same (except possibly for leading zeros if you want to expand it to the full size).

That's because 123 fits just fine into a char. A signed char can hold at least the values -127 thru 127 (two's complement can handle -128, but the ISO standard allows for one's complement and sign/magnitude as two other encodings for negative numbers, neither of which can handle -128), an unsigned char at least 0 thru 255.

Assuming an 8-bit byte and 4-byte int:

Value  HexInt  HexChar                 BinaryInt                 BinaryChar
-----  ------  -------  ---------------------------------------  ----------
123      7B      7B     0000 0000 0000 0000 0000 0000 0111 1011   0111 1011

The binary value 0111 1011 is calculated as:

0111 1011
 ||| | ||
 ||| | |+-   1
 ||| | +--   2
 ||| |
 ||| +----   8
 ||+------  16
 |+-------  32
 +--------  64
           ===
           123

If you were to attempt to place a value into an 8-bit char that was larger than 8 bits, the least significant bits are usually the ones preserved.

char xyz = 0x1234; // results in xyz being given the value 0x34.

This is detailed in C99, section 6.3.1.3/2 and /3:

1/ When a value with integer type is converted to another integer type other than _Bool, if the value can be represented by the new type, it is unchanged.

2/ Otherwise, if the new type is unsigned, the value is converted by repeatedly adding or subtracting one more than the maximum value that can be represented in the new type until the value is in the range of the new type.

3/ Otherwise, the new type is signed and the value cannot be represented in it; either the result is implementation-defined or an implementation-defined signal is raised.


Decimal 123 is 0x7b in hex and 0111 1011 in binary regardless of the original type. The only times the type gets in the way is if there is overflow (eg. int x = 48273; char y = (char)x;). Occasionally, display of a signed char and an unsigned char may differ.


It would be the same. 0x7b hex , 0111 1011

Note that if your integers are 4 bytes, its bits are 0000 0000 0111 1011 though.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜