Decimal to binary with visualisation in python

martin

I need to create function to convert decimal numbers to binary, and visualise that process in this way:

for example 15:

15|1
7 |1
3 |1
1 |1

number 15 in binary is 1111 (from bottom to up).

I keep trying to do that, but I don't even know, than my function is correct. I know that python has a "bin" built in function, but its not possible to use it for that visualisation (or am I wrong?).

My last code:

FinalList = []
dividedList = []

number = 29
while number != 1:
    divided = number%2
    number = int(number/2)
    FinalList.append(divided)
    dividedList.append(number)
    print(divided)

if number == 0:
    print(0)
    FinalList.append(divided)
    dividedList .append(0)
else:
    print(1)
    FinalList.append(1)
    dividedList.append(1)


print(FinalList)
print(dividedList)

Can someone help me with that?

my output for number 230:

[0, 1, 1, 0, 0, 1, 1, 1]

[115, 57, 28, 14, 7, 3, 1, 1]

and should be:

[0, 1, 1, 0, 0, 1, 1, 1]

[230,115, 57, 28, 14, 7, 3, 1]
Sampath

Hope this helps!

Minor change to your code

FinalList = []
dividedList = []

number = 29

print(bin(number))

while number != 1:
    _new_number, divided =  divmod(number, 2)
    print("{}|{}".format(number, divided))
    number = _new_number
    FinalList.append(divided)
    dividedList.append(number)

if number == 0:
    print('0|0')
    FinalList.append(divided)
    dividedList .append(0)
else:
    print('1|1')
    FinalList.append(1)
    dividedList.append(1)


print(FinalList[::-1]) # should match with the `bin` result
print(dividedList)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Decimal to binary Half-Precision IEEE 754 in Python

How can I convert bytes object to decimal or binary representation in python?

Algorithm to convert a 2d binary list to decimal numbers in Python

Python program for converting decimal to binary

Decimal to Binary function, Python

How to convert decimal to binary value of rows in multindex dataframe (python)?

Decimal to binary python algorithm

Simple function to convert decimal number to binary in Python

How can I convert decimal to binary values in python?

Python - Converting Binary (list) to Decimal

Converting Decimal Number To Binary In Python 3

Basic Binary to Decimal Conversion Program not working (Python)

What are the necessary requirements for coding decimal to binary [Python]?

Binary To Decimal

binary to decimal using recursion,python

How to convert a decimal number into a binary number using STACK in python

Efficient visualisation in Python

how to convert decimal to binary by using repeated division in python

writing a 1024 bits length decimal number in a binary file with python

convert 32 bit, binary number to decimal in python

Python 3.6 converting 8 bit binary to decimal

Converting decimal numbers to binary with functions in python

Binary to Decimal conversion in Python

How to create a Python Decimal, Binary, Octal and HexaDecimal Converter with MENU?

convert a binary number (consists of the sign, mantissa and exponent ) to decimal number in python

String Index Error for Binary to Decimal Python Solution

Convert 32 bit binary to a decimal in python

Numpy arrax of integers from decimal to binary in python

18-bit Signed Binary number to Decimal in Python