[Tutor] More confusion on conversion

Roger Merchberger zmerch at 30below.com
Thu Oct 30 13:37:21 EST 2003


At 13:19 10/30/2003 -0500, Vicki Stanfield wrote:
>Okay, don't ask me why because the answer I got was "for historical
>reasons", but I have to take the length of something (a decimal value) and
>convert it to its hexadecimal equivalent and then convert that value to
>its hexadecimal equivalent padded to 2 places. The result is something
>like this:
>
>length = 10
>convert 10 to A
>convert A to 0x41
>padded version = 0x30 0x41
>
>When I try to convert the decimal 10 using either binascii.hexlify, I am
>told that it wants a single value instead of the two digit 10. How does
>one convert a 10 (decimal value) to its hex equivalent? I don't want the
>hex equivalent of 1 followed by the hex equivalent of 0. The example above
>is exactly what I must achieve.

I'm not *exactly* sure if this is what you want, but try this snippet & see 
if it's what you want to do:

length = 10                     # original length
hex_length = '%02x' % length    # length converted to 2place hex
individ_hexchar = []            # array to hold individual hex chars
for i in hex_length:            # loop thru hex_length...
   individ_hexchar.append(ord(i))  # Get the ordinal (ASCII) of each

print individ_hexchar           # See if this is what you want!


Anyway, I'm still a partial Python n00b... but I'm learning quick! (Working 
on a module that will yank ID3v1 & ID3v2 tags from .mp3 files... I have it 
working in theory, but it needs some cleanup...
;-)

Hope this helps,
Roger "Merch" Merchberger

--
Roger "Merch" Merchberger -- sysadmin, Iceberg Computers
zmerch at 30below.com

What do you do when Life gives you lemons,
and you don't *like* lemonade?????????????




More information about the Tutor mailing list