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

2014-09-20 Thread Peter Otten
Nagy László Zsolt wrote: > >>> bytes/str.ljust() >>> >> def pad(b, n=16, c=b"\x0f"): >>> ... length = (len(b)+n-1)//n*n >>> ... return b.ljust(length, c) >> Thanks! > One more question. How do I create a single char binary string from a > number? E.g. > > >>> bytes([65]) > b'A' > >

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

2014-09-20 Thread Nagy László Zsolt
bytes/str.ljust() def pad(b, n=16, c=b"\x0f"): ... length = (len(b)+n-1)//n*n ... return b.ljust(length, c) Thanks! One more question. How do I create a single char binary string from a number? E.g. >>> bytes([65]) b'A' It seems to be wrong again. The bytes constructor requires a

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

2014-09-20 Thread Peter Otten
Nagy László Zsolt wrote: > 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? By the way, you can repeat bytes (and strings) by multip

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

2014-09-20 Thread Nagy László Zsolt
bytes/str.ljust() def pad(b, n=16, c=b"\x0f"): ... length = (len(b)+n-1)//n*n ... return b.ljust(length, c) Thanks! -- This message has been scanned for viruses and dangerous content by MailScanner, and is believed to be clean. -- https://mail.python.org/mailman/listinfo/python-li

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

2014-09-20 Thread Steven D'Aprano
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 "", line 1, in >File "", line 1, in

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

2014-09-20 Thread Peter Otten
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 "", line 1, in >File "", line 1, in