Simple function to convert decimal number to binary in Python

MM1

I'm trying to convert a decimal number in binary but I can't seem to understand why my code doesn't work. Before checking it I need to precise that:

  • I am not allowed to use modulo, division, powers, or any libraries
    that might help me to accomplish my goal.
  • I can use multiplication, addition, subtraction as well as comparisons.

That said, here is my code:

def binary_conv(n):
    p1 = 1
    p2 = 1
    lst = list()
    counter1 = 0
    counter2 = 0

    while p1 <= n:
        p1 *=2
        counter1 += 1

    len_bin = counter1
    lst = len_bin*[0]
    counter1 -= 1

    while n > 0:
        p2 *= 2
        counter2 += 1
        if (p2*2) == p1:
            lst[counter1-counter2] = 1
            p1 = p2
            p2 = 1
            n = n-p1
            counter2= 0


    print(lst)

If you need more clarifications, don't hesitate to ask.

MM1

I actually found the answer, here it is for those interested:

def binary_conv(n):
    p1 = 1
    p2 = 1
    lst = list()
    counter1 = 0
    counter2 = 0

    while p1 <= n:
        p1 *= 2
        counter1 += 1

    len_bin = counter1
    lst = len_bin*[0]
    counter1 -= 1

    while n > 0:
        p2 *= 2
        counter2 += 1
        if (p2 * 2) > n:
            if n == 1:
                lst[-1] = 1
                break
            lst[counter1 - counter2] = 1
            p1 = p2
            p2 = 1
            n = n - p1
            counter2 = 0

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

convert 32 bit, binary number to decimal in python

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

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

Convert negative binary number to decimal

Decimal to Binary function, Python

C program to convert a decimal number to binary string

Convert decimal to binary with billions number in C

How to convert a decimal number to binary in Swift?

Convert decimal number provided in String to the binary

My function to convert decimal number into binary in (C) is producing wrong output for (some numbers)! [Without Arrays]

Built-in function to convert binary to decimal in JavaScript

How to convert a Decimal to Number(with decimal numbers) in Python

How to convert a sympy decimal number to a Python decimal?

How to convert a decimal number to a binary number using recursive functions?

How to convert a binary fraction number into decimal fraction number?

How to convert a binary fraction number into decimal fraction number in R?

Convert decimal number to binary number and change one index

Convert 32 bit binary to a decimal in python

Converting Decimal Number To Binary In Python 3

How do I convert a very long binary number to decimal?

decimal number convert to 16bit binary (C)

convert a number or string to binary number in python?

Python function that turns a number into binary

Convert from decimal to any base number in Python

convert decimal to binary

Convert binary strings into decimal

Convert decimal to binary in Matlab?

Convert binary string to decimal

Convert binary vector to decimal