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


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

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

Started byNagy László Zsolt <gandalf@shopzeus.com>
First post2014-09-20 12:00 +0200
Last post2014-09-20 22:12 +1000
Articles 2 — 2 participants

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


Contents

  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

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

FromNagy László Zsolt <gandalf@shopzeus.com>
Date2014-09-20 12:00 +0200
Subjectpad binary string with a given byte value (python 3)
Message-ID<mailman.14163.1411207223.18130.python-list@python.org>
 >>> 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.

[toc] | [next] | [standalone]


#78101

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2014-09-20 22:12 +1000
Message-ID<541d6f39$0$29977$c3e8da3$5496439d@news.astraweb.com>
In reply to#78099
Nagy László Zsolt wrote:

>  >>> 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?

You don't need to write your own function, just use the relevant string and
bytes methods. Use the ljust and rjust (left and right justify) methods:

To add padding on the left, justify on the right, and vise versa:

py> "Hello".ljust(20, "\x0f")
'Hello\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f'
py> "Hello".rjust(20, "\x0f")
'\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0fHello'

(The default fill character is space.)

The same works for bytes, except you have to make sure your fill char is
also a byte:

py> b"Hello".rjust(20, b"\x0f")
b'\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0fHello'
py> b"Hello".ljust(20, b"\x0f")
b'Hello\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f'



-- 
Steven

[toc] | [prev] | [standalone]


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


csiph-web