Skip to content

Instantly share code, notes, and snippets.

@jayspeidell
Last active January 10, 2021 21:51
Show Gist options
  • Save jayspeidell/48e625b1c84c771c41401ac9dfbc8584 to your computer and use it in GitHub Desktop.
Save jayspeidell/48e625b1c84c771c41401ac9dfbc8584 to your computer and use it in GitHub Desktop.
Step-by-step binary, dex, and decimal conversion
# This is a learning tool to illustrate the steps of converting from
# one number system to another.
# dec2hex(int) Convert a decimal number to hex step-by-step.
# dec2bin(int) Convert binary number to decimal step-by-step.
# bin2dec(string) Convert a binary fraction to a decimal fraction. 12 decimal places.
# bin2float(string) Convert a binary fraction to a decimal fraction with scientific notation.
# float2bin(float) Convert a decimal fraction to a binary fraction.
def low_scientific(n, pnt=True):
'''
Convert numbers less than one to scientific notation.
Returns the normalized float and decimal exponent.
'''
exp = 0
while n < 1:
n *= 10
exp += 1
if pnt:
print("%.5f x 10^-%d" % (n, exp))
return n, exp
def dec2hex(n):
'''
Convert a decimal integer to hex step-by-step.
'''
print("==========")
num = []
letters = {10: "A", 11: "B", 12: "C", 13: "D", 14: "E", 15: "F"}
r = 0;
while (n != 0):
r = n % 16
if (r<10):
num.append(r)
print("%d %% 16 = %d" % (n, r) )
else:
num.append(letters[r])
print("%d %% 16 = %d (%s)" % (n, r, letters[r]) )
print("%d / 16 = %d" % (n, int(n/16)))
n = int(n / 16)
print()
print(' '.join(map(str, num[::-1])))
print("==========")
def dec2bin(n):
'''
Convert decimal integer to binary step by step.
'''
print("==========")
num = []
r = 0;
while (n != 0):
r = n % 2
num.append(r)
print("%d %% 2 = %d" % (n, r) )
print("%d / 2 = %d" % (n, int(n/2)))
n = int(n / 2)
print()
print(' '.join(map(str, num[::-1])))
print("==========")
def bin2dec(binary):
'''
Convert binary integer to decimal step-by-step.
'''
sum = 0
n = 0
for digit in binary[::-1]:
if (digit == '1'):
print("1*2^%d or %d" % (n, 2**n))
sum += 2**n
else:
print("0*2^%d or 0" % n)
n+=1
print()
print("Total: %d" % sum)
def bin2float(fraction, start=1):
'''
Convert a binary fraction to a decimal fraction. Simply prints 12 decimals.
Input is a string with the binary values to the right of the decimal.
Fraction is a string of binary digits, start is the binary exponent.
Pass a positive exponent even though it's negative.
'''
print("==========")
print("Binary fraction: %s" % fraction)
n = start
sum = 0.0
for digit in fraction:
if (digit == "1"):
print("2^-%d = %.12f" % (n, (2**-n)))
sum += 2**-n
print("Subtotal = %.12f" % sum)
else:
print("Passing 0 digit at 2^-%d" % n)
n += 1
print()
print("Total: %.12f" % sum)
print("==========")
def bin2longfloat(fraction, start=1):
'''
Convert a binary fraction to a decimal fraction with scientific notation.
Input is a string with the binary values to the right of the decimal.
This one works with very small numbers.
Fraction is a string of binary digits, start is the binary exponent.
Pass a positive exponent even though it's negative.
'''
print("==========")
print("Binary fraction: %s, start at 2^%d" % (fraction, start))
print()
n = start
sum = 0.0
for digit in fraction:
if (digit == "1"):
temp_val, temp_exp = low_scientific(2**-n, pnt=False)
print("Add 2^-%d = %.8f x 10^-%d" % (n, temp_val, temp_exp))
sum += 2**-n
print("Subtotal = %.8f x 10^-%d" % (low_scientific(sum, pnt=False)))
else:
print("Passing 0 digit at 2^-%d" % n)
n += 1
print()
print("Total: %.50f" % sum)
print("Total = %.8f x 10^-%d" % (low_scientific(sum, pnt=False)))
print("==========")
def float2bin(fraction, limit=0):
'''
Convert a decimal fraction to a binary fraction.
Input is a decimal number between 0 and 1. Basically the right side of the decimal point.
'''
print("==========")
num = []
for i in range(1, limit+6):
print("%f * 2 = %f" % (fraction, fraction * 2))
fraction *= 2
if (fraction >= 1):
print("Append '1' to binary, subtract 1")
num.append(1)
fraction -= 1
else:
print("Append '0' to binary")
num.append(0)
if (i == limit):
num.append(" Trailing: ")
print()
print(''.join(map(str, num)))
print("==========")
# I'm looking for softwware engineering roles. Check out jayspeidell.com and hire me today!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment