Python Forum
bytearray weirdness or newbee newness
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
bytearray weirdness or newbee newness
#1
As a project to gauge my learning of python, and one that holds my interest, I’m playing with a program that reads an ancient, arcane, and obscure file system on a SD card, after several python code rewrites to test different structures, the bytearray() function has gotten me a long way down the road, but now it has confused me.

I can’t remember how I got on to this, although the variable/structure created by bytearray(), type() as a bytearray, but the array elements type() as int, conceptually, an intarray? Everything runs fine, can read and show directories, read file bitmaps (ancient is block in use) tables, basically just creeping forward.

Can anyone straighten out my misconception of bytearray() array elements being type() int, what do I have goofed up here?

curbie
Reply
#2
A bytearray is an array of int. There is no such thing as a byte in Python. The main difference between list[int] and bytearray is you cannot put negative values or values > 255 in a bytearray.
x = bytearray((1, 2, 3, 4))
x.append(128)
print(x)
x.append(256)
Error:
bytearray(b'\x01\x02\x03\x04\x80') Traceback (most recent call last): File "test.py", line 4, in <module> x.append(256) ValueError: byte must be in range(0, 256)
If you are trying to read binary data into Python I suggest you take a look at struct.

https://docs.python.org/3/library/struct.html
Reply
#3
Thanks again deanhystad,

So just so I understand, a bytearray is a intarray with the int(s) constrained to values 0-255?

Maybe I'm using bytearray improperly, it's the core of my program (gets all blocks), so if there's a better way, better for me to rewrite as early as possible!

Code to get all Alpha Micro blocks:
def get_am_blk(disk, block_no=0):
    """Read a single AlphaMicro block (zero offset) plus one for hidden block,
     of the specified SD or disk image.

    Keyword arguments:
    disk = the string containing the name of the disk image file to read.
           or the full file path to disk image file, or device containing
           the disk image to read.
    block_no = is the integer sector number to read (default: 0).
    """
    # File operations using `with` syntax. To read image block and reduce file handling efforts.
    with open(disk, 'rb') as fp:        # open file read binary, set file descriptor
        fp.seek((block_no + 1) * BLOCK) # seek file to byte ((sector + 1 hidden block) * BLOCK)
        block = bytearray(fp.read(BLOCK))  # read "BLOCK" (512) bytes (STD sector) into byte array
        # print(type(block))            # type block = bytearray, block[500] = int
        block = endian(block)           # swap endian
    return block                        # return am block
Any good URLs to struct docs and examples?
Reply
#4
Look at the struct link from my previous post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  send bytearray over UART trix 9 1,530 Sep-30-2024, 01:08 PM
Last Post: trix
  Value error when converting hex value to bytearray shubhamjainj 7 13,093 Mar-20-2023, 05:30 PM
Last Post: Skaperen
  appending to a bytearray Skaperen 21 33,048 Mar-19-2023, 11:05 PM
Last Post: Skaperen
  Split Bytearray into separate Files by Hex delimter lastyle 5 6,354 Mar-09-2023, 07:49 AM
Last Post: bowlofred
  file handling Newbee question middlecope 2 1,568 Jan-18-2023, 03:09 PM
Last Post: middlecope
  Save multiple Parts of Bytearray to File ? lastyle 1 1,528 Dec-10-2022, 08:09 AM
Last Post: Gribouillis
  bytearray object - why converting to ascii? Chepilo 2 4,563 Nov-21-2022, 07:25 PM
Last Post: Chepilo
  Bytearray substitution Vismuto 1 3,426 Apr-14-2020, 09:18 AM
Last Post: TomToad
  Newbee Question chas777 4 2,976 Apr-01-2020, 06:52 PM
Last Post: chas777
  Useless Newbee craigpusey 2 98,155 Mar-04-2020, 02:13 PM
Last Post: craigpusey

Forum Jump:

User Panel Messages

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