Tuesday, 28 November 2017

After a long break, a binary converter

Despite the lack of activity, I haven't completely given up on this blog. Things are getting busier with Open University modules, so more of my mental energy has gone into academic study rather than self-motivated learning about Python.

However, I have come up with something short and sweet. This is something of a step away from some of the more challenging topics I was considering. Classes, objects, regular expressions and SQLite are subjects I am intending to get onto, but currently I am not really confident about posting stuff that I have only just got my head around.

So here I present a program that convert numbers from decimal to binary:
#!/usr/bin/python3

print ("Binary Converter Program")
num = input("Please enter decimal number to be converted: ")
num = int(num)

comparator = 1
digcount = 0
while comparator <= num:
    digcount += 1
    comparator *= 2

print ('Number of digits is', digcount)
print ('Chosen number is less than', comparator)

binstring = ''
while digcount > 0:
    digcount -= 1
    comparator = 2** digcount
    if num - comparator >= 0:
        binstring = binstring + '1'
        num = num - comparator
    else:
        binstring = binstring + '0'
    
if binstring == '':
    binstring = '0'
print ('Binary string is', binstring)
This gives the outputs:
>>>
RESTART: C:\Users\666\Dropbox\Misc Programming\Python\python3\binaryconverter.py
Binary Converter Program
Please enter decimal number to be converted:
19
Number of digits is 5
Chosen number is less than 32
Binary string is 10011

>>>
RESTART: C:\Users\666\Dropbox\Misc Programming\Python\python3\binaryconverter.py
Binary Converter Program
Please enter decimal number to be converted:
187
Number of digits is 8
Chosen number is less than 256
Binary string is 10111011

>>>
RESTART: C:\Users\666\Dropbox\Misc Programming\Python\python3\binaryconverter.py
Binary Converter Program
Please enter decimal number to be converted:
2017
Number of digits is 11
Chosen number is less than 2048
Binary string is 11111100001
>>> 
I admit the  printout of "number of digits" and "chosen number is less than" is diagnostic, and probably not useful for any user.
Similarly, I am not sure if this is the simplest, most elegant or most Pythonic way of doing this task, but  it seems to work so that's good enough for me. If there is a better way, feel free to let me know.
A few notes about how I have done this:

  • The digcount variable is first used to record how many binary digits the converted number will have. It is then used to keep track of which column / digit in the binary printout is being tested to see if it should be a '1' or '0'
  • The comparator variable also serves a dual purpose. Firstly to see if the number being converted is bigger or smaller than a power of 2 (i.e. 2 ** x). Secondly while the number is reduced by having powers of 2 subtracted from it, the comparator is used to see if that gives a '1' or a '0' for that digit/power of 2. 
  • There is a lot of use of the +=, -+ and *= operators, to change a variable by a certain amount. 
  • Finally the outputted binary 'number' is actually a string. It just seemed more convenient that way.  


No comments:

Post a Comment