, 16진수() function은 정수를 해당하는 16진수 형식으로 변환하는 데 사용되는 Python3의 내장 함수 중 하나입니다. 16진수는 컴퓨터 과학 및 프로그래밍에서 일반적으로 사용되는 16진법 번호 매기기 시스템입니다. 이 기사에서는hex()>10진수를 16진수 형식으로 변환하는 함수 및 사용법 파이썬.
Python Hex() 함수 구문
통사론: 16진수(x)
매개변수: x - 정수( 정수 물체)
보고: 16진수 문자열을 반환합니다.
Python Hex() 함수 예
그만큼hex()>Python의 함수는 10진수를 해당하는 16진수 표현으로 변환하는 데 사용됩니다. 정수를 인수로 사용하고 16진수 값을 나타내는 문자열을 반환합니다.
파이썬3
문자열을 int로 변환
decimal_number>=> 999> hexadecimal_value>=> hex>(decimal_number)> print>(hexadecimal_value)> |
>
>
산출
0x3e7>
ASCII 및 부동 소수점 값에 대한 Python Hex() 함수
Python에서는 다음을 사용할 수 있습니다.hex()>변환하는 함수 아스키 문자를 해당 16진수 표현으로 변환합니다. 그만큼 ord()>기능 문자의 ASCII 값을 얻는 데 사용됩니다.hex()>해당 값을 16진수로 변환하는 데 적용됩니다.
파이썬3
# 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()를 사용하여 비트 연산을 수행할 수 있습니다. 비트 연산은 데이터의 이진 표현 내에서 개별 비트를 조작하는 연산입니다. 파이썬은 다양한 기능을 제공합니다 비트 연산 , AND, OR, XOR, NOT 등과 같은
파이썬3
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에서는 16진수를 10진수로, 16진수를 2진수로, 16진수를 문자열로, 16진수를 8진수로 변환하는 등 다양한 변환에 Hex()를 사용할 수 있습니다. 예는 다음과 같습니다.
파이썬3
문자를 정수로
# 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를 보여줍니다.
파이썬3
# 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.> '''> |
>
arp - 명령
>
산출
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진수로 변환합니다.