Python Forum
Four sequential bytes; Need to remove high order bit in each to get correct output
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Four sequential bytes; Need to remove high order bit in each to get correct output
#1
I am writing program to manipulate the contents of an .MP3 file. In reading the header in
binary(byte) mode, decoding the header Size field is giving me problems. See ID3.org. The definition
of the field is
ID3v2 size = 4 * %0xxxxxxx

I have been away from programming for about 15 years and need to relearn the basics.

Not looking for code here, just a pointer on how to remove 4 bits from the inside of the field.

Huh

Thanks,

GrandSean
Reply
#2
So sounds like you want to treat the 4 bytes as a single 28-bit number. In that case I would:

* initialize total to zero
* starting with the high-order byte, loop over the four bytes
** shift the total left by 7 bits (total << 7)
** add the current byte & 127 to the total.
Reply
#3
(Jan-20-2021, 05:00 AM)bowlofred Wrote: So sounds like you want to treat the 4 bytes as a single 28-bit number. In that case I would:

* initialize total to zero
* starting with the high-order byte, loop over the four bytes
** shift the total left by 7 bits (total << 7)
** add the current byte & 127 to the total.

Okay, I get the concept but am having a problems translating it to code.
Just to make sure we are on the same page, I read 4 consecutive bytes (total of 32 bits) from
a file and I need to make a 28 bit number by removing the most significant bit from each byte.
Reply
#4
Quote:Example:

255 (%11111111) encoded as a 16 bit synchsafe integer is 383
(%00000001 01111111).

length_ss = 383
length_bytes = length_ss.to_bytes(4, 'big')
length = 0
for byte in length_bytes:
    length = length * 128 + byte
print(f'Synchsafe {length_ss} = integer{length}')
Reply
#5
Here's someone that has coded up both encoding and decoding in python.

https://stuffivelearned.org/doku.php?id=misc:synchsafe
Reply
#6
Thank you both for the pointers.

GrandSean
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Sequential number for rows retrieved and storing the Primary UKey to the line number GYKR 2 555 Aug-22-2023, 10:14 AM
Last Post: GYKR
  Create a sequential number (counter) and reset at each duplicate Mekala 0 1,703 Sep-20-2020, 05:02 AM
Last Post: Mekala
  attribute error instead of correct output MaartenRo 2 2,147 Aug-28-2020, 10:22 AM
Last Post: Larz60+
  Visual Studio-remove Output clutter mds 14 8,695 Jun-04-2020, 11:47 PM
Last Post: mds
  Finding sequential invoice william888 0 1,342 Mar-24-2020, 12:41 AM
Last Post: william888
  Sequential color background swisha 2 1,986 Mar-05-2020, 03:43 PM
Last Post: Larz60+
  GPIO time in HIGH LOW boris_za 1 2,229 Dec-07-2019, 01:48 PM
Last Post: Larz60+
  Not getting correct output supriya0307 5 5,467 Sep-10-2019, 10:28 PM
Last Post: ichabod801
  Not quite getting the correct Output from a function twinpiques 3 2,622 Aug-04-2019, 11:53 PM
Last Post: twinpiques
  understanding output of bytes/raw data rootVIII 3 2,711 Aug-01-2019, 01:00 PM
Last Post: rootVIII

Forum Jump:

User Panel Messages

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