Binary Numbers and Use in Computers + How to make a base conversion program in python

Arvin Seifipour
The Startup
Published in
3 min readMay 18, 2020

--

Before the rise of microprocessors in the 1970’s, analog circuits were used to do complex computations. Analog signals have a continuously variable flow that makes their transmission accurate, but they lack noise immunity and are not desirable for long distance transmission. Analog Computers also required gigantic components.

Bombe, a general purpose computer made by Alan Turing to crack the german Enigma Code during World War II
Bombe, a general purpose computer made by Alan Turing to crack the german enigma code during World War II

Digital signals in contrast have finite possible values at any given point in time. Modern digital circuits use binary patterns for transmission that can only take a value of ‘1’(very high flow of electrons) or ‘0’(extremely low flow of electrons), which can represent data or logical functionality. Nano scale semiconductor devices used on modern central processing units, called transistors, are used to make logic gates that perform boolean logic on inputs by switching the signal to ‘ON’ or ‘OFF’ to represent a new output. Binary Numbers can represent any number, but unlike the traditional decimal system, they only use 2 digits, ‘0’ and ‘1’, to represent numbers.

Here’s a visual representation of decoding binary to decimal and encoding decimal to binary:

It can be a little confusing at first to do this by hand and not be sure if the converted value is right, especially when alphabet letters get involved when we run out of digits in bases higher than 10, but we can make a universal converter python app to check our answers. Open a blank python file on an online python playground or your local editor. We’re gonna start by defining a ‘decode’ function that takes a number in the base range(2,36) in string form and returns the equivalent number decoded to decimal:

import stringdef decode(digits, base):  #check for valid input
assert 2 <= base <= 36, ‘base is out of range: {}’.format(base)

exponent = 0
#counter points to last digit in digits
counter = len(digits)-1
sum=0
#iterates over digits from right to left to add converted decimal
while counter>=0:
if digits[counter].isdigit():
sum+=int(digits[counter])*(base**exponent)
else:
sum+=int(digits[counter],base=base)*(base**exponent)
counter-=1
exponent+=1

return sum

Now we can convert any number to decimal, but in order to make the universal base converter, we need to make an encoding functionality that converts base 10 numbers to any number in the (2,36) base range. After that, we can use base 10 as a key for other base conversions. So let’s make the encode function, that takes a number in base 10 and returns the equivalent number encoded to any base:

def encode(number, base):
assert 2 <= base <= 36, ‘base is out of range: {}’.format(base)
#empty string for encoded number
encodedValue = “”

#Another iteration to perform division to calculate remainder
while number > 0:
number, remainder = divmod(number, base)
if remainder >= 10:
encodedValue += chr(remainder + 87)
else:
encodedValue += str(remainder)
encodedValue = encodedValue[::-1] return encodedValue

And finally we can make our converter function that uses decode and encode as helper reusable functions to convert numbers from a given base(base1) to another(base2) :

def convert(digits, base1, base2):

return encode(decode(digits, base1), base2)

You can test your functions by running a test case like this:

print(convert("10000",2,36))
#console should print 'G' after running the file with "python3 filename.py" command

Sources:

“Binary Number System (Examples, Solutions).” Www.onlinemathlearning.com, www.onlinemathlearning.com/binary-number-system.html.

“Java Program for Decimal to Binary Conversion.” GeeksforGeeks, 13 Feb. 2018, www.geeksforgeeks.org/java-program-for-decimal-to-binary-conversion/.

The Turing Bombe in Bletchley Park, www.rutherfordjournal.org/article030108.html.

--

--