binary to decimal conversion

ezra ezra at pagic.net
Mon Mar 27 07:15:54 EST 2000


>

I write three functions:
import string

#-------------------------------
__doc__ = 'Data convert function.   written by ezra.\012'

def Int2Bin(di):
     chars = ['0','1']
     do = ''
     q = 0
     while di >= 2:
          q = di % 2
          do = chars[q] + do
          di = di / 2
     do = chars[di] + do
     if len(do)%4 >= 1 and len(do)%4 <= 3 : do = '0'*(4 - len(do)%4) + do
     return do
# End of def Int2Bin(di)


def Int2Oct(di):
     chars = ['0','1','2','3','4','5','6','7']
     do = ''
     q = 0
     while di >= 8:
          q = di % 8
          do = chars[q] + do
          di = di / 8
     do = chars[di] + do
     if len(do)%2 == 1 : do = '0' + do
     return do
# End of def Int2Oct(di)


def Int2Hex(di):
     chars =
['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F']
     do = ''
     q = 0
     while di >= 16:
          q = di % 16
          do = chars[q] + do
          di = di / 16
     do = chars[di] + do
     if len(do)%2 == 1 : do = '0' + do
     return do
# End of def Int2Hex(di)

#-------------------------------

--
洪偉能(Wei-Neng Hung)
國立中央大學資訊工程研究所,高速通訊與計算實驗室
High-Speed Communication and Computing Laboratory,NCU
E-mail : ezra at csie.ncu.edu.tw





More information about the Python-list mailing list