Coding/Python 삽질기
[Python] 진법 변환
chobocho
2016. 12. 27. 01:06
진법 변환 하기
내장 함수를 이용한 진법 변환
num = 255 print int(num) # 255 print hex(num) # 0xff print oct(num) # 0377 print bin(num) # 0b11111111 |
재귀를 이용한 진법 변환
def convertBase(n, base): N = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" q, r = divmod(n, base) if q == 0: return N[r] else: return convertBase(q, base) + N[r] |
루프를 이용한 진법 변환
def convertBase(n, base): N = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" answer = "" q = n while q!=0: q, r = divmod(q, base) answer = N[r] + answer return answer |
Update : 2017.6.28