Quantcast

[issue14694] Option to show leading zeros for bin/hex/oct

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

[issue14694] Option to show leading zeros for bin/hex/oct

STINNER Victor

New submission from endolith <[hidden email]>:

Suggestion: Add an option to bin/hex/oct functions to format binary output with a minimum fixed width, including leading zeros.  Also might be useful for hex and oct.

Currently, bin(18) produces '0b10010'

with this change, something like bin(18, foo=8) would produce '0b00010010'


Examples of people wanting this:

http://stackoverflow.com/questions/3258330/converting-from-hex-to-binary-without-losing-leading-0s-python

http://stackoverflow.com/questions/1002116/can-bin-be-overloaded-like-oct-and-hex-in-python-2-6

http://stackoverflow.com/a/1425558/125507

https://www.linuxquestions.org/questions/programming-9/in-python-printing-leading-zero-for-hex-numbers-0-through-f-719426/

----------
components: Interpreter Core
messages: 159623
nosy: endolith
priority: normal
severity: normal
status: open
title: Option to show leading zeros for bin/hex/oct
type: enhancement
versions: Python 2.7

_______________________________________
Python tracker <[hidden email]>
<http://bugs.python.org/issue14694>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/lists%2B1322467933539-512619%40n6.nabble.com

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

[issue14694] Option to show leading zeros for bin/hex/oct

STINNER Victor

Mark Dickinson <[hidden email]> added the comment:

-1. str.format already does this quite effectively;  I don't see a real need to complicate the bin, hex and oct signatures.

>>> '{:016b}'.format(324)
'0000000101000100'
>>> '{:016o}'.format(324)
'0000000000000504'
>>> '{:016x}'.format(324)
'0000000000000144'

----------
nosy: +mark.dickinson

_______________________________________
Python tracker <[hidden email]>
<http://bugs.python.org/issue14694>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/lists%2B1322467933539-512619%40n6.nabble.com

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

[issue14694] Option to show leading zeros for bin/hex/oct

STINNER Victor
In reply to this post by STINNER Victor

Changes by Mark Dickinson <[hidden email]>:


----------
versions: +Python 3.3 -Python 2.7

_______________________________________
Python tracker <[hidden email]>
<http://bugs.python.org/issue14694>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/lists%2B1322467933539-512619%40n6.nabble.com

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

[issue14694] Option to show leading zeros for bin/hex/oct

STINNER Victor
In reply to this post by STINNER Victor

Changes by Mark Dickinson <[hidden email]>:


----------
stage:  -> needs patch

_______________________________________
Python tracker <[hidden email]>
<http://bugs.python.org/issue14694>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/lists%2B1322467933539-512619%40n6.nabble.com

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

[issue14694] Option to show leading zeros for bin/hex/oct

STINNER Victor
In reply to this post by STINNER Victor

Mark Dickinson <[hidden email]> added the comment:

I'm rejecting this: the functionality is already there in str.format, and there's little to be gained by adding another way to do it.

----------
nosy: +eric.smith
resolution:  -> rejected
status: open -> closed

_______________________________________
Python tracker <[hidden email]>
<http://bugs.python.org/issue14694>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/lists%2B1322467933539-512619%40n6.nabble.com

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

[issue14694] Option to show leading zeros for bin/hex/oct

STINNER Victor
In reply to this post by STINNER Victor

Eric V. Smith <[hidden email]> added the comment:

I agree with Mark.

This can also be done slightly more efficiently with plain format():

>>> format(324, "016b")
'0000000101000100'
>>> format(324, "016o")
'0000000000000504'
>>> format(324, "016x")
'0000000000000144'

And with either format() or str.format(), you can add the appropriate prefix:
>>> format(324, "#016b")
'0b00000101000100'
>>> format(324, "#016o")
'0o00000000000504'
>>> format(324, "#016x")
'0x00000000000144'

I don't see ever adding all of the possible options to bin(), etc.

----------

_______________________________________
Python tracker <[hidden email]>
<http://bugs.python.org/issue14694>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/lists%2B1322467933539-512619%40n6.nabble.com

Loading...