Nombres entiers, valeurs binaires, hexadécimales et octales : conversion
>>> nbre = 18 >>> a = bin (nbre) >>> a '0b10010' >>> b = hex (nbre) >>> b '0x12' >>> c = oct (nbre) >>> c '0o22' >>> d = int (0b10010) >>> d 18 >>> e = int (0x12) >>> e 18 >>> f = int (0o22) >>> f 18 >>> type (a) <class 'str'> >>> g = 0b10010 >>> type (g) <class 'int'> >>> g 18 >>>
Utilisation des fonctions intégrées int ( ), bin ( ), hex ( ), oct ( )
Attention ! :
>>> nbre2 = 12.3 >>> h = bin (nbre2) Traceback (most recent call last): File "<pyshell#19>", line 1, in h = bin (nbre2) TypeError: 'float' object cannot be interpreted as an integer >>> i = hex (nbre2) Traceback (most recent call last): File "<pyshell#20>", line 1, in i = hex (nbre2) TypeError: 'float' object cannot be interpreted as an integer