Вопрос

For example, how are the following statements equal to each other?

a) 0116 = 0x4E

b) 97 = 00141 = 0x0061

c) 45 = 0x2d= 0055

Ive tried looking it up but it still doesn't make sense to me. Thanks.

Это было полезно?

Решение

If it begins with 0x or 0X, it is a hexadecimal number, ie. base 16.

Else if it begins with just 0, it is an octal number, ie. base 8.

Else it is a decimal number, ie. base 10.

(the following is non-standard - a gcc extension)

if it begins with 0b, it is a binary number, ie. base 2.

Другие советы

number starting with 0 is octal number number starting with 0x is hexadecimal

we need to differentiate between two different number system and in programming we do it this way

if you want to verify these equalities you can solve it by yourself or you can use online converter. i am giving you link below

http://calc.50x.eu/

Numbers starting with a 0 are in octal. Octal digits between 0 and 7. Counting in octal goes:
0, 1, 2, 3, 4, 5, 6, 7, 10, 11, 12, 13, 14, 15, 16, 17, 20, ...

Numbers starting with a 0x or 0X are in hexadecimal (hex). Hex uses digits and alpha values. Counting in Hex goes:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F, 10, ...

So how is 01168 == 0x4E16?

The C compiler will see the 0 in 0116 and know to interrupt the number as octal. The compiler can check different number systems against each other where as we, mostly humans, need to convert them to something we can understand better.

Converting Octal to Decimal can be done as the Σ (for i=0 to n) of the function: aix8i. In your example:
1x82 = 64
1x81 = 8
6x80 = 6
64+8+6 = 7810

Converting Hex to Decimal can be done with nearly the same formula but you need to convert each letter to the decimal equivalent for the ai portion of: aix16i.
4x161 = 64
14x160 = 14 //E16 == 1410
64+14 = 7810

Clearly 78==78, the compilter just doesn't need to do the conversion first to see that.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top