Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #198060 > unrolled thread

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

Started byJohann 'Myrkraverk' Oskarsson <johann@myrkraverk.invalid>
First post2026-09-24 06:42 +0800
Last post2026-09-25 23:25 +0800
Articles 5 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  ''.join([bin(i)[2:].zfill(8) for i in flag]) Johann 'Myrkraverk' Oskarsson <johann@myrkraverk.invalid> - 2026-09-24 06:42 +0800
    Re: ''.join([bin(i)[2:].zfill(8) for i in flag]) ram@zedat.fu-berlin.de (Stefan Ram) - 2026-09-23 23:09 +0000
      Re: format_binary Lawrence D’Oliveiro <ldo@nz.invalid> - 2026-09-24 02:01 +0000
        Re: format_binary Jon Ribbens <jon+usenet@unequivocal.eu> - 2026-09-24 06:57 +0000
      Re: ''.join([bin(i)[2:].zfill(8) for i in flag]) Johann 'Myrkraverk' Oskarsson <johann@myrkraverk.invalid> - 2026-09-25 23:25 +0800

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

FromJohann 'Myrkraverk' Oskarsson <johann@myrkraverk.invalid>
Date2026-09-24 06:42 +0800
Subject''.join([bin(i)[2:].zfill(8) for i in flag])
Message-ID<nnd$45c195ac$47172e6f@35c50d4e9a8c02a9>
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 ( ;; ) _:;

[toc] | [next] | [standalone]


#198061

Fromram@zedat.fu-berlin.de (Stefan Ram)
Date2026-09-23 23:09 +0000
Message-ID<bin-20260924000855@ram.dialup.fu-berlin.de>
In reply to#198060
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.

[toc] | [prev] | [next] | [standalone]


#198062 — Re: format_binary

FromLawrence D’Oliveiro <ldo@nz.invalid>
Date2026-09-24 02:01 +0000
SubjectRe: format_binary
Message-ID<11920a6$29ujm$1@dont-email.me>
In reply to#198061
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

[toc] | [prev] | [next] | [standalone]


#198063 — Re: format_binary

FromJon Ribbens <jon+usenet@unequivocal.eu>
Date2026-09-24 06:57 +0000
SubjectRe: format_binary
Message-ID<slrn11b9if4.2kd.jon+usenet@raven.unequivocal.eu>
In reply to#198062
On 2026-09-24, Lawrence D’Oliveiro <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.

[toc] | [prev] | [next] | [standalone]


#198067

FromJohann 'Myrkraverk' Oskarsson <johann@myrkraverk.invalid>
Date2026-09-25 23:25 +0800
Message-ID<qnwtS.229030$RI1.198964@fx04.ams4>
In reply to#198061
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web