Computer >> 컴퓨터 >  >> 프로그램 작성 >> Python

Python에서 Decimal을 다른 기수로 빠르게 변환

<시간/>

Python에는 10진수를 2진수, 8진수 및 16진수와 같은 다른 기수로 변환하는 몇 가지 쉬운 방법이 있습니다. 예를 들어 숫자가 10진수로 19이면 2진수로 10011, 8진수로 23, 16진수로 13으로 표시됩니다.

결과에서 Binary, Octal 및 Hexadecimal의 숫자 앞에 각각 0b, 0o, 0x가 표시됩니다. 이러한 표기법을 사용하여 수의 밑수를 쉽게 결정할 수 있습니다.

예시 코드

#using bin(), oct(), hex() functions
x = 242
print('The number {} in binary form: {}'.format(x, bin(x)))
print('The number {} in octal form: {}'.format(x, oct(x)))
print('The number {} in hexadecimal form: {}'.format(x, hex(x)))

출력

The number 242 in binary form: 0b11110010
The number 242 in octal form: 0o362
The number 242 in hexadecimal form: 0xf2