RUBY
파이썬을 이용한 진법 변환 본문
파이썬을 이용한 진법 변환
진법
진법이란, 특정 숫자 몇 개를 사용하여 수를 표시하는 방법이다.
10진수를 2진수로 변환하기
dNum = 30
print('2진수: {}'.format(bin(dNum)))
print('Type of bin(dNum): {}'.format(type(bin(dNum))))
dNum = 30
print('2진수: {0:#b}'.format(dNum))
10진수를 8진수로 변환하기
dNum = 30
print('8진수: {}'.format(oct(dNum)))
print('Type of oct(dNum): {}'.format(type(oct(dNum))))
dNum = 30
print('8진수: {0:#o}'.format(dNum))
10진수를 16진수로 변환하기
dNum = 30
print('16진수: {}'.format(hex(dNum)))
print('Type of oct(dNum): {}'.format(type(hex(dNum))))
dNum = 30
print('16진수: {0:#x}'.format(dNum))
X진수를 10진수로 변환하기
dNum = 30
print('2진수(0b11110) -> 10진수({})'.format(int('0b11110', 2)))
print('8진수(0o36) -> 10진수({})'.format(int('0o36', 8)))
print('16진수(0x1e) -> 10진수({})'.format(int('0x1e', 16)))
2진수를 X진수로 변환하기
dNum = 30
print('2진수(0b11110) -> 8진수({})'.format(oct(0b11110)))
print('2진수(0b11110) -> 10진수({})'.format(int(0b11110)))
print('2진수(0b11110) -> 16진수({})'.format(hex(0b11110)))
8진수를 X진수로 변환하기
dNum = 30
print('8진수(0o36) -> 2진수({})'.format(bin(0o36)))
print('8진수(0o36) -> 10진수({})'.format(int(0o36)))
print('8진수(0o36) -> 16진수({})'.format(hex(0o36)))
16진수를 X진수로 변환하기
dNum = 30
print('16진수(0x1e) -> 2진수({})'.format(bin(0x1e)))
print('16진수(0x1e) -> 8진수({})'.format(oct(0x1e)))
print('16진수(0x1e) -> 10진수({})'.format(int(0x1e)))
'프로그래밍 언어 > Python' 카테고리의 다른 글
등비 수열(파이썬 출력) (0) | 2023.01.14 |
---|---|
등차 수열(파이썬 출력) (0) | 2023.01.14 |
공배수와 최소공배수(파이썬출력) (0) | 2023.01.14 |
공약수, 최대공약수(파이썬 출력) (0) | 2023.01.14 |
소인수, 소인수분해(파이썬 출력) (0) | 2023.01.14 |
Comments