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 decimal to binary

C program to convert a decimal number to binary string

How to convert a decimal number to binary in Swift?

Convert binary vector to decimal

Convert binary string to decimal

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

Convert from decimal to any base number in Python

Convert negative binary number to decimal

decimal number convert to 16bit binary (C)

Convert decimal number to binary number and change one index

Decimal to Binary function, Python

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

Converting Decimal Number To Binary In Python 3

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

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

Convert decimal to binary in Matlab?

Convert binary strings into decimal

convert 32 bit, binary number to decimal in python

Built-in function to convert binary to decimal in JavaScript

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

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

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

Convert decimal to binary with billions number in C

Convert 32 bit binary to a decimal in python

convert a number or string to binary number in python?

Python function that turns a number into binary

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

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

Convert decimal number provided in String to the binary