Terry Carroll wrote: > I'm assuming you want the same serial # that shows up when you do a > DIR on the CD, e.g.: > > >dir d: > Volume in drive D is 050512_1752 > Volume Serial Number is 8A73-780D > > Here's some code: > > >>>>import win32api >>>>CD_Info = win32api.GetVolumeInformation("D:/") >>>>serno = CD_Info[1] >>>>serno > > -1972144115 > >>>>"%X" % serno > > '-758C87F3' > >>>>"%X" % -serno > > '758C87F3' > > This is as far as I can get. the hex string, in this case 758C87F3 is the > two's complement of the serial number that shows up when I do a "dir d:": > > >dir d: > Volume in drive D is 050512_1752 > Volume Serial Number is 8A73-780D > > Note that: > 758C 87F3 > + 8A73 780D > ========= > 1 0000 0000 > > Put another way, if you flip every bit in the 758C87F3 string and add one, > you'll get the 8A73780D serial no. > > I hope someone more artful than I can show a quick and easy way to convert > either '758C87F3' or -1972144115 to the '8A73780D' that is your goal.
What is happening here is that Python is treating the bit pattern '8A73780D' as a signed integer. A common way to represent signed integers is with two's complement notation, so Python interprets '8A73780D' as the number -1972144115. When you convert this to hex using %x formatting, the sign is preserved and you see the representation '-758C87F3'. To get the desired representation, you have to find a way to make Python interpret the number as an unsigned integer. One way to do this is to convert it to a long integer, then mask off the sign bits: >>> i = long(-1972144115) & 0xFFFFFFFF >>> i 2322823181L >>> '%x' % i '8a73780d' As you noted, John Fouhy's number doesn't have the high bit set. So it is interpreted as a positive number. Masking the sign bits has no effect because they are already zeros: >>> i=0x49BC31DB >>> i 1237070299 >>> i = long(i) & 0xFFFFFFFF >>> i 1237070299L >>> '%x' % i '49bc31db' More about two's complement notation here: http://en.wikipedia.org/wiki/Two%27s_complement Kent _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor