Search code examples
pythonbytebit

How is this 4 bytes?


I am trying to understand how to manually read bytes from a file. In this case, the file is a bitmap image. This b'\x7fcq\x7f' and this b'ds~d' represent 4 bytes of data from my image file. Can someone explain to me how this can be or just how to manually read bytes?


Solution

  • Its just 4 bytes. What you see is python's bytes representation of that data where ascii characters are used for the byte. Except when there isn't a viewable ascii for that byte, then you get a hex representation like \x7f. But this is just python showing the 4 bytes for humans.

    Another way to view it is as a list of integers, one per byte

    >>> data = b'\x7fcq\x7f'
    >>> list(data)
    [127, 99, 113, 127]