Python decimal is converted into arbitrary binary

Source: Internet
Author: User
Tags ord

Remember college days, participate in the school's programming contest, in which the problem is:

Write a function that converts decimal into 16 binary.

Looks very simple a problem, finally unexpectedly did not realize, think all feel ashamed ah, go back online a search, that is quite easy things; after five or six years, the work has been used in Java, recently learned Python, so suddenly think of this problem, use Python to achieve this question. The following two methods are used to implement each:

First, the circulation

def Decimaltonbasebynormal (Decimalvar, Base):    templist = []    temp = Decimalvar    i = 0    while (Temp > 0):        ord = temp% base if (Ord > 9): #如果余数大于9, the letter means ord = Chr (ord-10) #把数字转换成 Character Templist.append (ord) temp = Int (temp/ base) i = i + 1 templist.reverse (); #print (templist) binary = "" For j in Range (len (templist)): binary = binary + str (templist[j]), print ("The decimal is:%d and after convering By%d base is%s "% (Decimalvar, base, binary))          

One of the things to note is that when the remainder is greater than 9 o'clock, to convert to a character representation; with a circular method, the idea is very clear, as for the conversion algorithm here is not much introduction, you can see reference [1]; The problem encountered in the implementation process is how to convert the number into a character; The conversion method is as follows:

Ord = Chr (+ (ORD-10))

Where Ord is the remainder, converted to a character starting with ASCII, which can be directly converted to characters using the CHR function in Python 3.3

Second, recursion

def Dectonbasebyrecursion (Dec, base):    if (dec = = 0):        return    dectonbasebyrecursion (int (dec/ Base)     ord = Dec% Base    if (Ord > 9): Ord = Chr (ord-10)) sys.stdout.write (str (o RD))      

Recursive method, the implementation of the code is very concise, but it is not so simple to understand, in the process of recursive implementation, encountered an output problem, Python 3.3, the function print default automatic line wrapping, the method of adding commas on the net, tried invalid, so directly using stdout output, This is going to be using import sys.

Main code:

Import sysdef main ():    decimal = eval (input ("Please input the decimal for converting binary:"))    base = Eval (Input ("Please input base:"))    decimaltonbasebynormal (decimal, Base)    dectonbasebyrecursion (decimal , base)   

Tool: Pyscripter

Python version 3.3

The above link is code.google, does not turn over is not to go, I toward "the great masterpiece" Ah!

Python just get started, only consider the results of the operation, without considering any performance problems, there is a wrong place, please correct me!

Full code

Of course python has a simpler function to output directly:

Binary bin ()

Octal Oct ()

Hex Hex ()

To be continued: arbitrary conversions into decimal

Python decimal is converted into arbitrary binary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.