|
|
| 2005-05-11 |
hex to string?
So I had a string I really needed to retrieve (a serial number that I appear to have misplaced, and which the software conveniently forgot when I swapped hard drives) which was stored in the app’s plist as a large ASCII-encoded hexadecimal number. Turning it into a string should have been easy, and I still maintain it is, but in my late-night torpor this is the best I could come up with:
In [41]: s = '30798C86...'
In [42]: l = [int(''.join(i), 16) for i in zip(s[::2], s[1::2])]
Out[42]: [48, 121, 140, 134, ...]
In [43]: struct.pack('B' * len(l), *l)
Out[43]: '0y\x8c\x86...'
at which point I realized that didn’t look anything like a serial number, so I’ll have to call my parents in the morning to see if they can find the box for said software (thanks, Apple). Still, there must be a better way to do this in Python without explicitly looping through the string.
Update: I asked on IRC, and got:
23:49 bdash> sabi: s.decode('hex') maybe?
23:49 sabi> !!!
23:49 sabi> yes, that's perfect
23:49 bdash> :-)
Thanks Mark.
23:24:32
|
comment []
|
trackback []
#
|
|
|
| May 2005 |
|---|
| Mo | Tu | We | Th | Fr | Sa | Su |
|---|
| | | | | | | 1 | | 2 | 3 | 4 | 5 | 6 | 7 | 8 | | 9 | 10 | 11 | 12 | 13 | 14 | 15 | | 16 | 17 | 18 | 19 | 20 | 21 | 22 | | 23 | 24 | 25 | 26 | 27 | 28 | 29 | | 30 | 31 | | | | | | | Apr 2005 | | Jun 2005 |
|---|
Computer architecture, medicine, usability, Mac OS X, Python, Cocoa and hiptop development, and my life.
| View RSS |
 | Email me |
|