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


Groups > comp.lang.python > #78099

pad binary string with a given byte value (python 3)

Date 2014-09-20 12:00 +0200
From Nagy László Zsolt <gandalf@shopzeus.com>
Subject pad binary string with a given byte value (python 3)
Newsgroups comp.lang.python
Message-ID <mailman.14163.1411207223.18130.python-list@python.org> (permalink)

Show all headers | View raw


 >>> BS = 16
 >>> pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS)
 >>> pad('L')
'L\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f'
 >>> pad(b'L')
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
   File "<stdin>", line 1, in <lambda>
TypeError: can't concat bytes to str

How do I write this function so it can pad byte strings? chr(charcode) 
creates a normal string, not a binary string.

I can figure out way for example this:

 >>> b'T'+bytearray([32])

but it just don't seem right to create a list, then convert it to a byte 
array and then convert it to a binary string. What am I missing?


-- 
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.

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


Thread

pad binary string with a given byte value (python 3) Nagy László Zsolt <gandalf@shopzeus.com> - 2014-09-20 12:00 +0200
  Re: pad binary string with a given byte value (python 3) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-09-20 22:12 +1000

csiph-web