Python Forum

Full Version: chr() for bytes
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
you can make a single character string with the specific code you want with chr() passing to it an int with the intended code and getting a return value a string of length 1 that has a character with that code.  is there a byte string version of this?  suppose you want to insert a byte into a byte string (or bytearray string) based on a code you have as an int.  for a string you might do:
    s = 'hello cruel world'
    p = s+chr(code)
while for a byte string, to avoid the error TypeError: can't concat bytes to str if mixing bytes and str with:
    s = b'hello cruel world'
    p = s+chr(code)
you might do:
    s = b'hello cruel world'
    p = s+bchr(code)
if bchr() existed.  does anything like that exist?
(Nov-25-2017, 05:46 AM)heiner55 Wrote: [ -> ]You could use the six package:
http://six.readthedocs.io/#binary-and-text-data

bytearray(six.int2byte(10)) does the job in both python2 and python3
you need to concatenate string to string:
code = 45
s = 'hello cruel world'
p = s + str(chr(code))
should work (not tested)
(Nov-25-2017, 03:29 AM)Skaperen Wrote: [ -> ]if bchr() existed.  does anything like that exist?
>>> s = b'hello cruel world'
>>> p = s + bchr(97)
>>> print(p)
b'hello cruel worlda'
>>> s = b'hello cruel world'
>>> p = s + bchr(9731)
>>> print(p)
b'hello cruel world\xe2\x98\x83'
>>> print(p.decode())
hello cruel world☃
The missing bchr() .
def bchr(arg):
    s = chr(arg)
    return s.encode()
(Nov-25-2017, 12:55 PM)Larz60+ Wrote: [ -> ]you need to concatenate string to string:
code = 45
s = 'hello cruel world'
p = s + str(chr(code))
should work (not tested)
right.

but if the string to be concatenated to is binary bytes, then i need to have a binary byte to do the concatenating with, or convert to string, do the concatenating with chr(), then convert back.  the problems with the latter method include knowing the right code to use with chr() to get the correct binary byte back.  a bchr() that does the same as chr() but produces a byte string, instead, is one to do the first method and get it right.  and yes, that would allow transparent unencoded conversion of one character from string to byte by doing bchr(ord(strchar)).
looks like there's a bchr() post #5
if bytearray is sufficient (usually it would be) then this (nothing to import) can be used:
Output:
lt1/forums /home/forums 2> py2 Python 2.7.12 (default, Nov 19 2016, 06:48:10) [GCC 5.4.0 20160609] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> bytearray([97]) bytearray(b'a') >>> f='foobar' >>> bytearray([ord(x) for x in f]) bytearray(b'foobar') >>> bytearray(range(97,123)) bytearray(b'abcdefghijklmnopqrstuvwxyz') >>> lt1/forums /home/forums 3> py3 Python 3.5.2 (default, Sep 14 2017, 22:51:06) [GCC 5.4.0 20160609] on linux Type "help", "copyright", "credits" or "license" for more information. >>> bytearray([97]) bytearray(b'a') >>> f='foobar' >>> bytearray([ord(x) for x in f]) bytearray(b'foobar') >>> bytearray(range(97,123)) bytearray(b'abcdefghijklmnopqrstuvwxyz') >>> lt1/forums /home/forums 4>
including transparent bytes/bytearray to string conversion without any decoding.
how about one that works over the whole range of bytes type values:

def bchr(v):
    return bytes([int(v)])
by type you mean signed / unsigned? what other type of 'byte' is there?
or are you talking about display method (number base)?
Pages: 1 2