logo

Pythonのhex()関数

hex() function は Python3 の組み込み関数の 1 つで、整数値を対応する 16 進形式に変換するために使用されます。 16 進数は、コンピューター サイエンスやプログラミングで一般的に使用される 16 進数の番号付けシステムです。この記事では、hex()>10 進数を 16 進形式に変換する関数とその使用法 パイソン。

Python Hex() 関数の構文

構文: 16進数(x)



パラメータ: x – 整数 ( 整数 物体)

戻り値: 16 進数の文字列を返します。

Python Hex() 関数の例

hex()>Python の関数は、10 進数を対応する 16 進数表現に変換するために使用されます。引数として整数を受け取り、16 進値を表す文字列を返します。



Python3






decimal_number>=> 999> hexadecimal_value>=> hex>(decimal_number)> print>(hexadecimal_value)>

>

>

出力

0x3e7>

ASCII および Float 値の Python Hex() 関数

Python では、hex()>変換する関数 アスキー 文字を対応する 16 進表現に変換します。の ord()>関数 文字の ASCII 値を取得するために使用され、その後hex()>値を 16 進数に変換するために適用されます。

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>))>

>

>

出力

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() 関数によるビット単位の演算の実行

Python では、Hex() を使用してビット単位の演算を実行できます。ビット単位の操作は、データのバイナリ表現内の個々のビットを操作する操作です。 Pythonはさまざまな機能を提供します ビット単位の演算 、AND、OR、XOR、NOT など。

Python3

bashの部分文字列




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))>

順序トラバーサル
>

>

出力

0x2 0x2a>

さまざまな変換のための Python Hex() 関数

Python では、Hex() を使用して、16 進数から 10 進数、16 進数から 2 進数、16 進数から文字列、16 進数から 8 進数へのさまざまな変換を行うことができます。以下に例を示します。

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'>))>

>

>

出力

入力バリアント 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>

入力バリアント 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() 関数の TypeError のデモ

Python は、浮動小数点値がパラメータとして渡された場合の TypeError を示します。

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.> '''>

>

>

出力

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>

エラーと例外

  Typeerror :    Returns Typeerror when anything other than   integer type constants are passed as parameters.>

アプリケーション
Hex() はすべての関数で使用されます。 標準変換 。たとえば、16 進数から 10 進数へ、16 進数から 8 進数へ、16 進数から 2 進数への変換です。