Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple Byte Question
#4
Thanks for the help, @buran. This forum has been most helpful to me.

Well I finally figured out the problem.
def get_ebml_id(file):
    byte = file.read(1)
    dec = ord(byte)
    if dec & 0b10000000: #1 byte long EBML ID
        return byte
    elif dec & 0b01000000: #2 bytes long EBML ID
        return struct.unpack('>H', byte + file.read(1))[0]
    elif dec & 0b00100000: #3 bytes long EBML ID
        return struct.unpack('>L', b'\0' + byte + file.read(2))[0]
    elif dec & 0b00010000: #4 bytes long EBML ID
        return struct.unpack('>L', byte + file.read(3))[0]
If the byte read is supposed to be one byte long, it returns a byte object. It was throwing off an otherwise correct script. So, I changed
if dec & 0b10000000: #1 byte long EBML ID
        return byte
...to
if dec & 0b10000000:  # 1 byte long EBML ID
        return int.from_bytes(byte, byteorder='big')
...and all works as expected. I have learned that bytes objects are definitely not integers.
Reply


Messages In This Thread
Simple Byte Question - by malonn - Aug-26-2018, 06:35 PM
RE: Simple Byte Question - by buran - Aug-26-2018, 06:42 PM
RE: Simple Byte Question - by malonn - Aug-26-2018, 07:50 PM
RE: Simple Byte Question - by malonn - Aug-29-2018, 12:14 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Very Beginner question on simple variables Harvy 1 259 Apr-12-2024, 12:03 AM
Last Post: deanhystad
  Simple Question - ' defined as "a". ?' Ryan012 10 1,729 May-27-2023, 06:03 PM
Last Post: Ryan012
  Very simple question about filenames and backslashes! garynewport 4 2,041 Jan-17-2023, 05:02 AM
Last Post: deanhystad
  Python Tkinter Simple Multithreading Question AaronCatolico1 5 1,673 Dec-14-2022, 11:35 PM
Last Post: deanhystad
  A simple "If...Else" question from a beginner Serena2022 6 1,805 Jul-11-2022, 05:59 AM
Last Post: Serena2022
  Simple arithmetic question ebolisa 5 2,130 Dec-15-2021, 04:56 PM
Last Post: deanhystad
  Simple code question about lambda and tuples JasPyt 7 3,445 Oct-04-2021, 05:18 PM
Last Post: snippsat
Big Grin question about simple algorithm to my problem jamie_01 1 1,715 Oct-04-2021, 11:55 AM
Last Post: deanhystad
  Simple question 1234 4 2,326 Dec-04-2020, 12:29 PM
Last Post: DeaD_EyE
  'utf-8' codec can't decode byte 0xe2 in position 122031: invalid continuation byte tienttt 12 11,728 Sep-18-2020, 10:10 PM
Last Post: tienttt

Forum Jump:

User Panel Messages

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