, Das Hex() Die Funktion ist eine der integrierten Funktionen in Python3, die zum Konvertieren einer Ganzzahl in die entsprechende Hexadezimalform verwendet wird. Hexadezimal ist ein Zahlensystem zur Basis 16, das häufig in der Informatik und Programmierung verwendet wird. In diesem Artikel werden wir das untersuchenhex()>Funktion und ihre Verwendung bei der Konvertierung von Dezimalzahlen in das Hexadezimalformat in Python.
Syntax der Python Hex()-Funktion
Syntax: hex(x)
Parameter: x – eine ganze Zahl ( int Objekt)
Kehrt zurück: Gibt eine hexadezimale Zeichenfolge zurück.
Beispiel für eine Python-Hex()-Funktion
Derhex()>Die Funktion in Python wird verwendet, um eine Dezimalzahl in die entsprechende hexadezimale Darstellung umzuwandeln. Es verwendet eine Ganzzahl als Argument und gibt eine Zeichenfolge zurück, die den Hexadezimalwert darstellt.
Python3
decimal_number>=> 999> hexadecimal_value>=> hex>(decimal_number)> print>(hexadecimal_value)> |
>
>
Ausgabe
0x3e7>
Python Hex()-Funktion für ASCII- und Float-Werte
In Python können wir das verwendenhex()>Funktion zum Konvertieren ASCII Zeichen in ihre entsprechende hexadezimale Darstellung um. Der ord()>Funktion wird verwendet, um den ASCII-Wert eines Zeichens abzurufen, und dannhex()>wird angewendet, um diesen Wert in einen Hexadezimalwert umzuwandeln.
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>))> |
>
>
Ausgabe
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>
Python Hex()-Funktion führt bitweise Operationen aus
In Python können wir Hex() verwenden, um bitweise Operationen auszuführen. Bitweise Operationen sind Operationen, die einzelne Bits innerhalb binärer Darstellungen von Daten manipulieren. Python bietet verschiedene bitweise Operationen , wie AND, OR, XOR, NOT usw.
Python3
SQL-Konkat
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))> |
>
>
Ausgabe
0x2 0x2a>
Python Hex()-Funktion für unterschiedliche Konvertierungen
In Python können wir Hex() für verschiedene Konvertierungen wie Hex in Dezimalzahl, Hex in Binärzahl, Hex in String und Hex in Oktal verwenden. Hier ist ein Beispiel:
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'>))> |
>
>
Ausgabe
Eingabevariante 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>
Eingabevariante 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>
Python Hex()-Funktion demonstriert TypeError
Python demonstriert TypeError, wenn Gleitkommawerte als Parameter übergeben werden.
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.> '''> |
>
Laufzeit Fehler
>
Ausgabe
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> Fehler und Ausnahmen
Typeerror : Returns Typeerror when anything other than integer type constants are passed as parameters.>
Anwendungen
Hex() wird in allen verwendet Standardkonvertierungen . Zum Beispiel die Konvertierung von Hexadezimal in Dezimal, Hexadezimal in Oktal und Hexadezimal in Binär.