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


Groups > comp.lang.python > #198060

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

Date 2026-09-24 06:42 +0800
Newsgroups comp.lang.python
From Johann 'Myrkraverk' Oskarsson <johann@myrkraverk.invalid>
Subject ''.join([bin(i)[2:].zfill(8) for i in flag])
Organization Watcom Pro Ltd.
Message-ID <nnd$45c195ac$47172e6f@35c50d4e9a8c02a9> (permalink)

Show all headers | View raw


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 ( ;; ) _:;

Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

''.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

csiph-web