Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
chr() for bytes
#1
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.
Reply
#2
You could use the six package:
http://six.readthedocs.io/#binary-and-text-data
Reply
#3
(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.
Reply
#4
you need to concatenate string to string:
code = 45
s = 'hello cruel world'
p = s + str(chr(code))
should work (not tested)
Reply
#5
(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()
Reply
#6
(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.
Reply
#7
looks like there's a bchr() post #5
Reply
#8
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.
Reply
#9
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.
Reply
#10
by type you mean signed / unsigned? what other type of 'byte' is there?
or are you talking about display method (number base)?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  replace bytes with other byte or bytes BigOldArt 1 10,531 Feb-02-2019, 11:00 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020