, hex() Funkce je jednou z vestavěných funkcí v Pythonu3, která se používá k převodu celého čísla do odpovídajícího hexadecimálního tvaru. Hexadecimální je systém číslování se základem 16 běžně používaný v informatice a programování. V tomto článku prozkoumámehex()>funkce a její použití při převodu desítkových čísel do hexadecimálního formátu v Krajta.
Syntaxe funkce Python Hex()
Syntax: hex(x)
Parametr: x – celé číslo ( int objekt)
Vrácení: Vrátí hexadecimální řetězec.
Příklad funkce Python Hex()
Thehex()>Funkce v Pythonu se používá k převodu desítkového čísla na jeho odpovídající hexadecimální reprezentaci. Vezme celé číslo jako argument a vrátí řetězec představující hexadecimální hodnotu.
Python3
decimal_number>=> 999> hexadecimal_value>=> hex>(decimal_number)> print>(hexadecimal_value)> |
>
>
Výstup
0x3e7>
Funkce Python Hex() pro ASCII a plovoucí hodnotu
V Pythonu můžeme použíthex()>funkci převést ASCII znaků do jejich odpovídající hexadecimální reprezentace. The ord()>funkce se používá k získání hodnoty ASCII znaku a potéhex()>se použije k převodu této hodnoty na hexadecimální.
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>))> |
>
>
Výstup
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>
Funkce Python Hex() Provádí bitové operace
V Pythonu můžeme použít Hex() k provádění bitových operací. Bitové operace jsou operace, které manipulují s jednotlivými bity v rámci binárních reprezentací dat. Python nabízí různé bitové operace , jako je AND, OR, XOR, NOT atd.
Python3
pole upozornění javascript
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))> |
>
>
Výstup
0x2 0x2a>
Funkce Python Hex() pro různé převody
V Pythonu můžeme použít Hex() pro různé převody, jako je hex na desítkové, hex na binární, hex na řetězec a hex na osmičku. Zde je příklad:
Python3
převést z řetězce na celé číslo java
# 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'>))> |
>
>
Výstup
vstupní varianta 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>
varianta vstupu 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>
Funkce Python Hex() Demonstrujte TypeError
Python demonstruje TypeError, když jsou jako parametry předány hodnoty s plovoucí desetinnou čárkou.
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.> '''> |
>
>
Výstup
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> Chyby a výjimky
Typeerror : Returns Typeerror when anything other than integer type constants are passed as parameters.>
Aplikace
Hex() se používá ve všech standardní převody . Například převod šestnáctkové soustavy na desítkovou, šestnáctkové na osmičkové a šestnáctkové na binární.