Quantcast
Channel: Convert int to binary string in Python - Stack Overflow
Viewing all articles
Browse latest Browse all 74

Answer by HKC72 for Python int to binary string?

$
0
0

Calculator with all neccessary functions for DEC,BIN,HEX:(made and tested with Python 3.5)

You can change the input test numbers and get the converted ones.

# CONVERTER: DEC / BIN / HEXdef dec2bin(d):    # dec -> bin    b = bin(d)    return bdef dec2hex(d):    # dec -> hex    h = hex(d)    return hdef bin2dec(b):    # bin -> dec    bin_numb="{0:b}".format(b)    d = eval(bin_numb)    return d,bin_numbdef bin2hex(b):    # bin -> hex    h = hex(b)    return hdef hex2dec(h):    # hex -> dec    d = int(h)    return ddef hex2bin(h):    # hex -> bin    b = bin(h)    return b## TESTING NUMBERSnumb_dec = 99numb_bin = 0b0111 numb_hex = 0xFF## CALCULATIONSres_dec2bin = dec2bin(numb_dec)res_dec2hex = dec2hex(numb_dec)res_bin2dec,bin_numb = bin2dec(numb_bin)res_bin2hex = bin2hex(numb_bin)res_hex2dec = hex2dec(numb_hex)res_hex2bin = hex2bin(numb_hex)## PRINTINGprint('------- DECIMAL to BIN / HEX -------\n')print('decimal:',numb_dec,'\nbin:    ',res_dec2bin,'\nhex:    ',res_dec2hex,'\n')print('------- BINARY to DEC / HEX -------\n')print('binary: ',bin_numb,'\ndec:    ',numb_bin,'\nhex:    ',res_bin2hex,'\n')print('----- HEXADECIMAL to BIN / HEX -----\n')print('hexadec:',hex(numb_hex),'\nbin:    ',res_hex2bin,'\ndec:    ',res_hex2dec,'\n')

Viewing all articles
Browse latest Browse all 74

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>