This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Need a way to make my own bytes
Type: crash Stage:
Components: Interpreter Core Versions: Python 3.0
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: benjamin.peterson, loewis, lopgok
Priority: normal Keywords:

Created on 2008-12-08 03:29 by lopgok, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (4)
msg77286 - (view) Author: jeff deifik (lopgok) Date: 2008-12-08 03:29
I want to make my own data of types bytes in order to write it out.
For example, I want to write out the bytes 0..9

#!/usr/bin/env python3.0
foo = b''
for i in range (0,10):
    foo += i
#sys.stdout.buffer.write(foo)

Here is the error:
Traceback (most recent call last):
  File "./x.py", line 4, in <module>
    foo += i
TypeError: can't concat bytes to int

I cannot find any function to convert the int i into something
that I can append to foo. I tried chr, which produced a string
typeerror. byte() was not defined. There must be a way to convert
an integral value to a bytes type.
msg77287 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2008-12-08 04:14
bytes([some_number]) should do the trick.
msg77288 - (view) Author: jeff deifik (lopgok) Date: 2008-12-08 04:45
Doesn't work.
#!/usr/bin/env python3.0

import sys
foo = b''

for i in range (0,10):
    foo += bytes(i)

sys.stdout.buffer.write(foo)

produces a binary file of 45 bytes. Here is a hex dump (the '.'
represent unprintable characters):
+000    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00     ................
+016    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00     ................
+032    00 00 00 00 00 00 00 00 00 00 00 00 00              .............

that is 45 bytes of 0.
msg77289 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2008-12-08 05:08
Please don't use the bug tracker to obtain help, but only to report
bugs. Use python-list@python.org to get help.

In this specific case, also read Benjamin's answer more carefully.
History
Date User Action Args
2022-04-11 14:56:42adminsetgithub: 48838
2008-12-08 05:08:18loewissetnosy: + loewis
messages: + msg77289
2008-12-08 04:45:25lopgoksetmessages: + msg77288
2008-12-08 04:14:37benjamin.petersonsetstatus: open -> closed
resolution: not a bug
messages: + msg77287
nosy: + benjamin.peterson
2008-12-08 03:29:29lopgokcreate