Notice
Recent Posts
Recent Comments
Link
«   2025/02   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28
Archives
Today
Total
관리 메뉴

RUBY

파이썬을 이용한 진법 변환 본문

프로그래밍 언어/Python

파이썬을 이용한 진법 변환

ruby-jieun 2023. 1. 14. 12:48

 

 

 

파이썬을 이용한 진법 변환

 


 

 

 

진법

진법이란, 특정 숫자 몇 개를 사용하여 수를 표시하는 방법이다.

 

 

 

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

 

 

Comments