• ''.join([bin(i)[2:].zfill(8) for i in flag])

    From Johann 'Myrkraverk' Oskarsson@johann@myrkraverk.invalid to comp.lang.python on Thu Sep 24 06:42:16 2026
    From Newsgroup: comp.lang.python

    Dear comp.lang.python,

    As the topic says, is

    ''.join([bin(i)[2:].zfill(8) for i in flag])

    really the most Pythonic way to do this? I mean, why are we converting
    each character in the flag [1] to a binary string, chunk off the first
    two bytes, and then fill up the front with zeros [2], just to join it
    with an empty string?

    I mean, isn't there a regular print-to-string-format function, something
    akin to C's sprintf(), that does this in a more succinct way? Can we do something like

    flag.expand_to_binary() ## ?

    Here, it should be obvious that `flag` is indeed of the A.S.C.I.I. sub-
    set, but this doesn't seem like it would work well, if the text is Uni-
    code and U.T.F.-8.

    This is a single line from the Cryptohack competition, but I am assuming
    the authors of the website are cryptographers, and not expert Pythonics.


    What do you think?

    [1] Here, `flag` is a string variable, if it's not clear from the con-
    text.

    [2] Or is it the back, as I haven't used zfill() before?
    --
    Johann | email: invalid -> com | http://www.myrkraverk.com/blog/
    I'm not from the Internet, I just work there. | via XS News https://bsky.app/profile/myrkraverk.bsky.social | for ( ;; ) _:;

    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From ram@ram@zedat.fu-berlin.de (Stefan Ram) to comp.lang.python on Wed Sep 23 23:09:14 2026
    From Newsgroup: comp.lang.python

    Johann 'Myrkraverk' Oskarsson <johann@myrkraverk.invalid> wrote or quoted:
    As the topic says, is
    ''.join([bin(i)[2:].zfill(8) for i in flag])
    really the most Pythonic way to do this?
    . . .
    I mean, isn't there a regular print-to-string-format function, something
    akin to C's sprintf(), that does this in a more succinct way?

    Like,

    ''.join( f'{i:08b}' for i in flag )

    ?

    Here, `flag` is a string variable, if it's not clear from the con-
    text.

    If "flag" would be a string, then "i" would be a string too,
    but "bin" expects an integer.


    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From Lawrence =?iso-8859-13?q?D=FFOliveiro?=@ldo@nz.invalid to comp.lang.python on Thu Sep 24 02:01:42 2026
    From Newsgroup: comp.lang.python

    On 23 Sep 2026 23:09:14 GMT, Stefan Ram wrote:

    Like,

    ''.join( f'{i:08b}' for i in flag )

    ?

    def format_binary(n : int) :
    assert n >= 0
    return \
    "".join \
    (
    chr(ord("0") + (n & 1 << i != 0))
    for i in range(math.floor(math.log(max(n, 1)) / math.log(2)), -1, -1)
    )
    #end format_binary

    Sample results:

    ldo@theon:python_try> ./format_binary 15
    15 => 0b1111
    ldo@theon:python_try> ./format_binary 16
    16 => 0b10000
    ldo@theon:python_try> ./format_binary 17
    17 => 0b10001
    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From Jon Ribbens@jon+usenet@unequivocal.eu to comp.lang.python on Thu Sep 24 06:57:40 2026
    From Newsgroup: comp.lang.python

    On 2026-09-24, Lawrence DrCOOliveiro <ldo@nz.invalid> wrote:
    On 23 Sep 2026 23:09:14 GMT, Stefan Ram wrote:

    Like,

    ''.join( f'{i:08b}' for i in flag )

    ?

    def format_binary(n : int) :
    assert n >= 0
    return \
    "".join \
    (
    chr(ord("0") + (n & 1 << i != 0))
    for i in range(math.floor(math.log(max(n, 1)) / math.log(2)), -1, -1)
    )
    #end format_binary

    Well, that is the absolute worst of all possible ways to do it, yes.
    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From Johann 'Myrkraverk' Oskarsson@johann@myrkraverk.invalid to comp.lang.python on Fri Sep 25 23:25:10 2026
    From Newsgroup: comp.lang.python

    On 9/24/2026 7:09 AM, Stefan Ram wrote:
    Johann 'Myrkraverk' Oskarsson <johann@myrkraverk.invalid> wrote or quoted:
    As the topic says, is
    ''.join([bin(i)[2:].zfill(8) for i in flag])
    really the most Pythonic way to do this?
    . . .
    I mean, isn't there a regular print-to-string-format function, something
    akin to C's sprintf(), that does this in a more succinct way?

    Like,

    ''.join( f'{i:08b}' for i in flag )

    ?

    Here, `flag` is a string variable, if it's not clear from the con-
    text.

    If "flag" would be a string, then "i" would be a string too,
    but "bin" expects an integer.

    Mea culpa, as Julius Caesar would say it. I missed the fact that the
    string in question has a b in front of it, like this.

    b'this is some string.'

    And that indeed turns the string into a sequence of bytes. So much to
    learn, so little Python.


    Best wishes, and happy Python.
    --
    Johann | email: invalid -> com | http://www.myrkraverk.com/blog/
    I'm not from the Internet, I just work there. | via Easynews.com https://bsky.app/profile/myrkraverk.bsky.social | for ( ;; ) _:;
    Federated at https://fed.brid.gy/bsky/myrkraverk.bsky.social
    --- Synchronet 3.22a-Linux NewsLink 1.2