integer woes...

sik0fewl xxdigitalhellxx at hotmail.com
Wed Feb 19 10:40:54 EST 2003


Luke McCarthy wrote:
> I have written an ID3v1 MP3 tag reader, but I have one slight problem. The
> track number is a two-byte integer and reads into Python like so:
> 
> 
>>>>tag.track
> 
> '\x00\x17'
> 
> (in this example, the track number is 23, which is hex 17)
> 
> My dilemma: how can I convert this into an integer?
> 
> int(tag.track) doesn't work!
> I get: ValueError: invalid literal for int():
> 
> Whoever designed these tags must be weird. Why was the track number a short
> int yet the year a string??? Why make artist, title and album 30 chars long
> yet make the comment 28 chars long???
> 
> Luke McCarthy

you can just use ord(tag[n]) (assuming you read everything into a 
string) where n is whatever byte \x17 is (I can't remember offhand). 
It's the last (30th) byte in the comment field so..

if comment[28] == 0:
	track = ord(comment[29])
	comment = comment[:28]

The reason comment is 28 chars long is because Track was added later in 
v1.1 and used space that was previously used by Comment. The way it 
works is that if the 29th byte or [28] of the comment field is 0, then 
the 30th byte or [29] is the track number. The two bytes *DON'T* combine 
to form the track number, although since the first is always 0, it will 
work out that way.

Hope this helps,
Ryan





More information about the Python-list mailing list