Function that takes a string representing a decimal number and returns it in binary format

Steff

I'm trying to get a function to take a string dec, representing a decimal number, for example "11" and I want said function to return a string, which contains the corresponding binary format, in this case "1011". So this is what I have so far:

def dec2bin(dec):

    dec = str("11")

    if dec > 1:
        binary(dec//2)
    return (dec % 2,end = "")

I'm very new to Python, so I'm not sure how to turn a number into a string (using str()) in the first place and how to make it return the corresponding binary value. Can anyone point me in the right direction?

Pynchia

Assuming you can use int to convert the string to an integer:

def dec2bin(snum):
    n = int(snum)
    bin_s = ['1' if (n >> b) & 1 else '0' for b in range(n.bit_length())]
    return ''.join(reversed(bin_s))

let's test it

>>> dec2bin('11')
'1011'

Basically, it scans the integer obtained from the string and checks every bit in it.

It shifts the number to the right and checks the least significant bit and-ing it with the value 1 (alternatively we could shift the mask to the left and leave the number unchanged).

The result of each bit-check is used to populate a list, which is then reversed and joined to form a string.

If using list comprehensions would make you look too cool to be true, use a for loop:

def dec2bin(snum):
    n = int(snum)
    bin_s = []
    for b in range(n.bit_length()):
        cur_bit = (n >> b) & 1
        sbit = chr(ord('0') + cur_bit)
        bin_s.append(sbit)
    return ''.join(reversed(bin_s))

Another solution, in case you can use python's builtin format function, would be:

def dec2bin(snum):
    return format(int(snum),"b")

Further note to clarify the algorithm (the main assumption is that we are only talking about unsigned integers):

Computers use binary representation of data (i.e. bits, zero and ones). Put one after the other, from right to left, they form a number in ascending powers of two, just like decimal digits do.

For example the number thirteen (13 is its representation in base ten: 1*10^1+3*10^0) is written as 1101 in binary (1*2^3+1*2^2+0*2^1+1*2^0) and stored in memory as bits within bytes (8-bits).

The LSB (Least Significant Bit) is the least powerful bit (binary digit), i.e. the rightmost one in 1101, because it weighs the least in terms of power of two.

Python allows a variable size for integers, that is why I use the bit_length method to find out how many bits are necessary to store that number. Other languages (e.g. C) allocate a predefined size to numbers, normally the same (or less) as the width of the registers the CPU provides.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Function that takes a string representing a decimal number and returns it in binary format

From Dev

Function that takes a string representing a filename as an argument. Function returns number of letters in the txt file

From Dev

function that takes iterables and returns a string

From Dev

Ensure a string representing a decimal number has a 0 before the "."

From Dev

C program to convert a decimal number to binary string

From Dev

How to convert string representing number i any format to integer

From Dev

A function that takes string parameter and returns integer pointer

From Dev

Binding String Format Number Commas & No Decimal Places

From Dev

Wont convert decimal number in string format to float

From Dev

Function, that takes string and number as parameter for creating filename

From Dev

PyParsing a string representing a function

From Dev

std::string stream parse a number in binary format

From Dev

How to convert 64 bit binary string in to a decimal number in tcl?

From Dev

Converting String of binary digits to decimal number... using recursion

From Dev

how to convert twos complement binary string to negative decimal number?

From Dev

Converting String of binary digits to decimal number... using recursion

From Dev

Function that returns number of significant figures after decimal point in specific formatting

From Dev

Convert binary string to decimal

From Dev

Format the number of decimal points

From Dev

Set format for decimal number

From Dev

Format number to string with custom group and decimal separator without changing precision

From Dev

How to parse decimal and hexadecimal string depending on number format to int

From Dev

Convert string representing a number of any base to number

From Dev

function that returns the number of letters that repeat in a string

From Dev

function that returns the number of letters that repeat in a string

From Dev

Method to convert binary number to decimal

From Dev

Converting decimal floating number to binary

From Dev

function that takes in and returns multiple values

From Dev

Filter string as decimal number

Related Related

  1. 1

    Function that takes a string representing a decimal number and returns it in binary format

  2. 2

    Function that takes a string representing a filename as an argument. Function returns number of letters in the txt file

  3. 3

    function that takes iterables and returns a string

  4. 4

    Ensure a string representing a decimal number has a 0 before the "."

  5. 5

    C program to convert a decimal number to binary string

  6. 6

    How to convert string representing number i any format to integer

  7. 7

    A function that takes string parameter and returns integer pointer

  8. 8

    Binding String Format Number Commas & No Decimal Places

  9. 9

    Wont convert decimal number in string format to float

  10. 10

    Function, that takes string and number as parameter for creating filename

  11. 11

    PyParsing a string representing a function

  12. 12

    std::string stream parse a number in binary format

  13. 13

    How to convert 64 bit binary string in to a decimal number in tcl?

  14. 14

    Converting String of binary digits to decimal number... using recursion

  15. 15

    how to convert twos complement binary string to negative decimal number?

  16. 16

    Converting String of binary digits to decimal number... using recursion

  17. 17

    Function that returns number of significant figures after decimal point in specific formatting

  18. 18

    Convert binary string to decimal

  19. 19

    Format the number of decimal points

  20. 20

    Set format for decimal number

  21. 21

    Format number to string with custom group and decimal separator without changing precision

  22. 22

    How to parse decimal and hexadecimal string depending on number format to int

  23. 23

    Convert string representing a number of any base to number

  24. 24

    function that returns the number of letters that repeat in a string

  25. 25

    function that returns the number of letters that repeat in a string

  26. 26

    Method to convert binary number to decimal

  27. 27

    Converting decimal floating number to binary

  28. 28

    function that takes in and returns multiple values

  29. 29

    Filter string as decimal number

HotTag

Archive