Timeline for Convert int to binary string in Python
Current License: CC BY-SA 3.0
14 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Jun 25, 2020 at 5:15 | review | Suggested edits | |||
| Jun 25, 2020 at 20:38 | |||||
| Jun 25, 2020 at 4:18 | comment | added | Dolf Andringa |
There is an issue here with negative numbers. @nate didn't clearly specify what the desired output was in that case, but purely in binary numbers, negative signs don't exist. So the most significant bit is generally used for the negative sign. Assuming we use 8bit integers, -37 would be 0b10100101. But with an unsigned integer, that value would be 165. So it is not this simple. The answer should reflect this.
|
|
| Jul 18, 2019 at 4:30 | comment | added | D. A. | f"{37:b}" in Python3.7 or later. | |
| Aug 14, 2018 at 21:51 | comment | added | Sparkler |
typically one would use 4/8/... bit representation: "{:08b}".format(37)
|
|
| S Oct 31, 2017 at 12:19 | history | suggested | OrenIshShalom | CC BY-SA 3.0 |
The input argument 10 looks like a binary representation itself ... an integer with digits in [2-9]+ is probably a better choice ...
|
| Oct 31, 2017 at 12:07 | review | Suggested edits | |||
| S Oct 31, 2017 at 12:19 | |||||
| Jul 28, 2016 at 23:53 | comment | added | tomasyany |
In this case the 0 in "{0:b}" can be dropped no? I mean, in the case where only one number is being formatted, it is correct to put "{:b}", isn't it?
|
|
| Dec 10, 2015 at 10:24 | comment | added | Martijn Pieters |
@mike: Or use the formatting specification. Add the number of digits with a leading 0 to the formatting string: format(10, '016b') formats to 16 digits with leading zeros.
|
|
| Dec 10, 2015 at 10:23 | comment | added | Martijn Pieters |
str.format() just to format one value is overkill. Go straight to the format() function: format(n, 'b'). There is no need to parse out the placeholder and match it to an argument, go straight for the value formatting operation itself. Only use str.format() if you need to place the formatted result in a longer string (e.g. use it as a template).
|
|
| S Jul 10, 2014 at 1:44 | history | suggested | Andrew Magee | CC BY-SA 3.0 |
Updated link to docs (was returning 404)
|
| Jul 10, 2014 at 1:26 | review | Suggested edits | |||
| S Jul 10, 2014 at 1:44 | |||||
| Dec 14, 2013 at 9:06 | review | Suggested edits | |||
| Dec 14, 2013 at 9:19 | |||||
| Apr 30, 2012 at 18:28 | vote | accept | Nate | ||
| Mar 31, 2009 at 3:17 | history | answered | Tung Nguyen | CC BY-SA 2.5 |