Posts: 4,647
Threads: 1,494
Joined: Sep 2016
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?
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Posts: 4,647
Threads: 1,494
Joined: Sep 2016
(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
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Posts: 4,647
Threads: 1,494
Joined: Sep 2016
(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))
.
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Posts: 4,647
Threads: 1,494
Joined: Sep 2016
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.
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Posts: 4,647
Threads: 1,494
Joined: Sep 2016
how about one that works over the whole range of bytes type values:
def bchr(v):
return bytes([int(v)])
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.