, L'esagono() function è una delle funzioni integrate in Python3, che viene utilizzata per convertire un numero intero nella corrispondente forma esadecimale. L'esadecimale è un sistema di numerazione in base 16 comunemente utilizzato in informatica e programmazione. In questo articolo esploreremo ilhex()>funzione e il suo utilizzo nella conversione dei numeri decimali in formato esadecimale in Pitone.
Sintassi della funzione Python Hex()
Sintassi: esa(x)
Parametro: x – un numero intero ( int oggetto)
Ritorna: Restituisce una stringa esadecimale.
Esempio di funzione Python Hex()
ILhex()>La funzione in Python viene utilizzata per convertire un numero decimale nella corrispondente rappresentazione esadecimale. Prende un numero intero come argomento e restituisce una stringa che rappresenta il valore esadecimale.
Python3
decimal_number>=> 999> hexadecimal_value>=> hex>(decimal_number)> print>(hexadecimal_value)> |
>
>
Produzione
0x3e7>
Funzione Python Hex() per ASCII e valore float
In Python, possiamo usare ilhex()>funzione da convertire ASCII caratteri nella corrispondente rappresentazione esadecimale. IL ord()>funzione viene utilizzato per ottenere il valore ASCII di un carattere, quindihex()>viene applicato per convertire quel valore in esadecimale.
Python3
# Python3 program to illustrate> # hex() function> print>(>'The hexadecimal form of 23 is '> >+> hex>(>23>))> > print>(>'The hexadecimal form of the '> >'ascii value is 'a' is '> +> hex>(>ord>(>'a'>)))> > print>(>'The hexadecimal form of 3.9 is '> >+> float>.>hex>(>3.9>))> |
>
>
Produzione
The hexadecimal form of 23 is 0x17 The hexadecimal form of the ascii value os 'a' is 0x61 The hexadecimal form of 3.9 is 0x1.f333333333333p+1>
Funzione Python Hex() Esegue operazioni bit a bit
In Python possiamo usare Hex() per eseguire operazioni bit a bit. Le operazioni bit a bit sono operazioni che manipolano singoli bit all'interno di rappresentazioni binarie di dati. Python fornisce vari operazioni bit per bit , come AND, OR, XOR, NOT, ecc.
Python3
ariete pothineni
hex_num1>=> 0x22> hex_num2>=> 0x0A> bitwise_and>=> hex_num1 & hex_num2> bitwise_or>=> hex_num1 | hex_num2> print>(>hex>(bitwise_and))> print>(>hex>(bitwise_or))> |
>
>
Produzione
0x2 0x2a>
Funzione Python Hex() per conversioni diverse
In Python, possiamo usare Hex() per diverse conversioni come esadecimale in decimale, esadecimale in binario, esadecimale in stringa ed esadecimale in ottale. Ecco un esempio:
Python3
# TypeConversion from decimal with base 10> # to hexadecimal form with base 16> # Taking input from user> # an integer with base 10> number>=> int>(>input>(>'Enter a number with base 10
'>))> # The choices present to the user> print>(>'a. Decimal to Hexadecimal '>)> print>(>'b. Decimal to Octal'>)> print>(>'c. Decimal to Binary'>)> # taking user input> print>(>'Enter your choice :- '>)> choice>=> input>()> # Running a variable choice> # Hexadecimal form if choice> # is set to 'a'> if> choice>is> 'a'>:> ># lstrip helps remove '0x' from the left> ># rstrip helps remove 'L' from the right,> ># L represents a long number> >print>(>'Hexadecimal form of '> +> str>(number)>+> >' is '> +> hex>(number).lstrip(>'0x'>).rstrip(>'L'>))> if> choice>is> 'b'>:> ># Octal representation is done> ># by adding a prefix '0o'> >print>(>'Octal form of '> +> str>(number)>+> >' is '> +> oct>(number).lstrip(>'0o'>).rstrip(>'L'>))> if> choice>is> 'c'>:> ># Binary representation is done by> ># the addition of prefix '0b'> >print>(>'Binary form of '> +> str>(number)>+> >' is '>+>bin>(number).lstrip(>'0b'>).rstrip(>'L'>))> |
>
>
Produzione
variante di ingresso a)
Enter a number with base 10 123 a. Decimal to Hexadecimal b. Decimal to Octal c. Decimal to Binary Enter your choice:- a Hexadecimal form of 123 is 7b>
variante di ingresso b)
Enter a number with base 10 123456789 a. Decimal to Hexadecimal b. Decimal to Octal c. Decimal to Binary Enter your choice:- a Hexadecimal form of 123456789 is 75bcd15>
La funzione Python Hex() dimostra TypeError
Python dimostra TypeError quando i valori in virgola mobile vengono passati come parametri.
Python3
# hex() accepts only integer values as parameters> print>(>'The hexadecimal form of 11.1 is '> >+> hex>(>11.1>))> '''> # The hexadecimal conversion of floating> # point integers can be done using the> # function float.hex()> print('The hexadecimal form of 11.1 is '> >+ float.hex(11.1))> # Output :> # The hexadecimal form of 11.1 is 0x1.6333333333333p+3> # Similarly, float.hex() throws a TypeError> # when integer values are passed in it.> '''> |
>
>
Produzione
ordinamento dell'elenco di array Java
Traceback (most recent call last): File '/home/7e1ac7e34362fd690cdb72cf294502e1.py', line 2, in print('The hexadecimal form of 11.1 is '+hex(11.1)) TypeError: 'float' object cannot be interpreted as an integer> Errori ed eccezioni
Typeerror : Returns Typeerror when anything other than integer type constants are passed as parameters.>
Applicazioni
Hex() è utilizzato in tutti i file conversioni standard . Ad esempio la conversione da esadecimale a decimale, da esadecimale a ottale e da esadecimale a binario.