Python Forum
int() function with bytearray()
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
int() function with bytearray()
#1
Hello,

I´m quite a noob and here is my problem with filling up a bytearray with integers (hex):

1. created an empty bytearray

>>> command = bytearray(3)
>>> print(command)
bytearray(b'\x00\x00\x00')
-> everything looks good!

2. added first byte to array

>>> command[0] = int(255)
>>> print(command)
bytearray(b'\xff\x00\x00')
-> everything looks good!

3. added another byte to the array

>>> command[1] = int(33)
>>> print(command)
bytearray(b'\xff!\x00')
-> why is there an "!" in the array??

4. added another byte to the array:
>>> command[1] = int(244)
>>> print(command)
bytearray(b'\xff\xf4\x00')
-> looks fine again!

>>> command[1] = int(132)
>>> print(command)
bytearray(b'\xff\x84\x00')
5. but if I´m adding lower number I´m lost again:

>>> command[1] = int(44)
>>> print(command)
bytearray(b'\xff,\x00')
-> why is there an "," in the bytearray?

Any ideas what is wrong with my approach?

When I´m adding integers between 33 and 126 to a bytearray with the same method I´m getting problems, with even higher integers it works nicely!

kr
jona

Update:

It seems to be a problem with print() function, not with int() to bytearray!

kr
jona
Reply
#2
The representation of the bytearray is just the chr() of each byte in the array. So if it's a printable ASCII character, that is displayed, otherwise the hexadecimal for the byte is displayed preceded by \x.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Value error when converting hex value to bytearray shubhamjainj 7 10,331 Mar-20-2023, 05:30 PM
Last Post: Skaperen
  appending to a bytearray Skaperen 21 13,324 Mar-19-2023, 11:05 PM
Last Post: Skaperen
  Split Bytearray into separate Files by Hex delimter lastyle 5 2,483 Mar-09-2023, 07:49 AM
Last Post: bowlofred
  Save multiple Parts of Bytearray to File ? lastyle 1 907 Dec-10-2022, 08:09 AM
Last Post: Gribouillis
  bytearray object - why converting to ascii? Chepilo 2 1,529 Nov-21-2022, 07:25 PM
Last Post: Chepilo
  Bytearray substitution Vismuto 1 2,561 Apr-14-2020, 09:18 AM
Last Post: TomToad
  Conversion needed from bytearray to Floating point braveYug 1 4,063 May-07-2018, 12:23 PM
Last Post: snippsat
  Windows DIB format in bytearray to image? dusca 2 2,717 Mar-28-2018, 10:35 PM
Last Post: dusca
  Bytearray questions mattps 2 14,337 Mar-25-2018, 06:54 AM
Last Post: mattps
  ByteArray outside while true Schampbakken 5 4,323 Feb-18-2018, 02:02 PM
Last Post: buran

Forum Jump:

User Panel Messages

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