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)

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!us.feeder.erje.net!fdn.fr!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <gandalf@shopzeus.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.004
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'string.': 0.05; 'binary': 0.07; 'clean.': 0.09; 'from:charset:iso-8859-2': 0.09; 'subject:string': 0.09; 'creates': 0.14; 'concat': 0.16; 'lambda': 0.16; 'mailscanner,': 0.16; 'missing?': 0.16; "skip:' 60": 0.16; 'typeerror:': 0.16; 'subject:python': 0.16; '>>>': 0.22; 'example': 0.22; 'header:User-Agent:1': 0.23; 'byte': 0.24; 'bytes': 0.24; 'string,': 0.24; 'this:': 0.26; 'function': 0.29; 'array': 0.29; 'scanned': 0.29; '"",': 0.31; 'file': 0.32; 'figure': 0.32; '(most': 0.33; 'subject: (': 0.35; 'subject:with': 0.35; "can't": 0.35; 'convert': 0.35; 'but': 0.35; 'believed': 0.36; 'to:addr:python-list': 0.38; 'list,': 0.38; 'recent': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'dangerous': 0.60; 'viruses': 0.61; 'charset:iso-8859-2': 0.64; 'pad': 0.84
Date Sat, 20 Sep 2014 12:00:05 +0200
From Nagy László Zsolt <gandalf@shopzeus.com>
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.6.0
MIME-Version 1.0
To python-list@python.org
Subject pad binary string with a given byte value (python 3)
Content-Type text/plain; charset=ISO-8859-2; format=flowed
Content-Transfer-Encoding 7bit
X-shopzeus-MailScanner-Information Please contact the ISP for more information
X-shopzeus-MailScanner-ID 9CDBF8897398.ACEE5
X-shopzeus-MailScanner Found to be clean
X-shopzeus-MailScanner-From gandalf@shopzeus.com
X-Spam-Status No
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.14163.1411207223.18130.python-list@python.org> (permalink)
Lines 26
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1411207223 news.xs4all.nl 2932 [2001:888:2000:d::a6]:52913
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:78099

Show key headers only | 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