How do you convert a number to 8 bit binary in python?

I am currently making a python program that is byte adder related, it will prompt the user to enter 2 integers, add them together and if it's within the range of the actual size of a byte (0 to 255) display that number and also give the corresponding value in its 8 bit binary form, for example, the calculation is equal to 1, show the binary number which python declares as the correct one.

This is the task that I am doing ~

"The program must check the input data for the data type permitted and the data value limitations (the value of the integer must not exceed the actual size of byte-coded integers, i.e. min 00000000 and max 11111111 in Base 2 or min 0 and max 255 in Base 10 for positive integers" Input in both binary and decimal format with conversion to binary

def add():
    Num1 = int(input("Input the first number between 0 and 255, the calculated answer must not be above 255: "))
    Num2 = int(input("Input the second number between 0 and 255, the calculated answer must not be above 255: "))

    calculatedanswer = Num1+Num2

    if calculatedanswer >= 0 and calculatedanswer <=255:
        print("The answer in decimal is" ,calculatedanswer,)

        **bin(calculatedanswer)**

    elif calculatedanswer < 0 and calculatedanswer >255:
        print("Please ensure the added numbers are above 0 and below 255")
        add()

This is my code so far, I have no trouble getting it to display the standard decimal number, but I really can't get the bin(calculatedanswer) to show the binary equivalent of it. I tried using this method that I found on YouTube.

I think my main problem here is my lack of understanding of how "bin" works on python as this is really the first time I am using it.

I have put asterisks around the line that I am having trouble with.

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Given a decimal number as input, the task is to write a Python program to convert the given decimal number into an equivalent binary number.
    Examples : 

    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010

    Method #1: Recursive solution

    DecimalToBinary(num):
            if num >= 1:
                DecimalToBinary(num // 2)
               print num % 2 

    How do you convert a number to 8 bit binary in python?

    Below is the implementation of the above recursive solution: 

    Python3

    def DecimalToBinary(num):

        if num >= 1:

            DecimalToBinary(num // 2)

        print(num % 2, end = '')

    if __name__ == '__main__':

        dec_val = 24

        DecimalToBinary(dec_val)

    Method #2: Decimal to binary using in-built function 

    Python3

    def decimalToBinary(n):

        return bin(n).replace("0b", "")

    if __name__ == '__main__':

        print(decimalToBinary(8))

        print(decimalToBinary(18))

        print(decimalToBinary(7))

    Method #3:Without in-built function

    Python3

    def decimalToBinary(n):

        return "{0:b}".format(int(n))

    if __name__ == '__main__':

        print(decimalToBinary(8))

        print(decimalToBinary(18))

        print(decimalToBinary(7))

    Quick Ninja Method: One Line Code to Convert Decimal to Binary with user input

    Python3

    or 

    Python3

    decNum = 4785

    print(bin(decNum)[2:])

    decNum1 = 10

    print(bin(decNum1)[2:])

    decNum2 = 345

    print(bin(decNum2)[2:])

    Output

    1001010110001
    1010
    101011001
    


    How do I convert a number to binary in Python?

    In Python, you can simply use the bin() function to convert from a decimal value to its corresponding binary value. And similarly, the int() function to convert a binary to its decimal value. The int() function takes as second argument the base of the number to be converted, which is 2 in case of binary numbers.

    How do I convert numbers to binary?

    Converting decimal integer to binary To convert integer to binary, start with the integer in question and divide it by 2 keeping notice of the quotient and the remainder. Continue dividing the quotient by 2 until you get a quotient of zero. Then just write out the remainders in the reverse order.

    What is an 8 bit binary number?

    An 8 bit binary number can represent a maximum of decimal 255= binary 11111111. Calculated as follows: 1*128 +1*64+1*32+1*16+1*8+1*4+1*2+1+1 = decimal 255. Here is another 8 bit binary number –01101011.