I am writing a script to rename and reorganize the mp3 on my iPod. For those who don't know, if you drag and drop your music from the iPod to a computer, there is a crazy F## folder structure and all the names are 4 random characters. I want to run the script and have it read the metadata ID3 tags, the rename and redo the folder structure based on the metadata.

The issue I am running into is that the ID3 tags store hex values with a \x prefix. Python requires a 0x prefix. I like to name my files with "Artist - Album - Track - Title". I can't read the track number because it is stored with a \x prefix.

Any ideas on how to convert/read/fix this?

8 Years
Discussion Span
Last Post by pyTony

Found the solution. You have to convert from binary to hex, then decode it and give it to an int constructor. Here's how to do it in one line:

from binascii import *
value = int(b2a_hex(input).decode(), 16)
for bunchofhex in [r'\x45\x23\x67',r'\x123456']:
    print bunchofhex
    bunchofhex = bunchofhex.split(r'\x')
    print bunchofhex
    value = int('0x'+''.join(bunchofhex),0)
    print '%i = 0x%x' %(value,value)
    print '-'*40

Edited by pyTony: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.