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

Answer by Bob Stein for Python int to binary string?

$
0
0

Summary of alternatives:

n=42assert  "-101010" == format(-n, 'b')assert  "-101010" == "{0:b}".format(-n)assert  "-101010" == (lambda x: x >= 0 and str(bin(x))[2:] or "-"+ str(bin(x))[3:])(-n)assert "0b101010" == bin(n)assert   "101010" == bin(n)[2:]   # But this won't work for negative numbers.

Contributors include John Fouhy, Tung Nguyen, mVChr, Martin Thoma. and Martijn Pieters.


Viewing all articles
Browse latest Browse all 74

Trending Articles



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