Coding/Python 삽질기2016. 12. 27. 01:06

진법 변환 하기



  1. 내장 함수를 이용한 진법 변환


num = 255


print int(num)   # 255

print hex(num)   # 0xff

print oct(num)   # 0377

print bin(num)   # 0b11111111



  1. 재귀를 이용한 진법 변환


def convertBase(n, base):

   N = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"

   q, r = divmod(n, base)

   if q == 0:

       return N[r]

   else:

       return convertBase(q, base) + N[r]



  1. 루프를 이용한 진법 변환


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

'Coding > Python 삽질기' 카테고리의 다른 글

Python for Windows Extensions  (0) 2017.06.21
단위분수를 소수로 변환하기  (0) 2016.09.07
[Notepad++] Python 실행하기  (0) 2016.08.28
Posted by chobocho