tags:

views:

24

answers:

1

(in case you're curious about motivation: this will be used in a scons build to generate a C file containing a GUID)

I found the question about generating a GUID in python. But I don't really know much about programming python. Could someone help me convert this to a string of the form

"{0x**, 0x**, 0x**, 0x**, 0x**, 0x**, 0x**, 0x**, 0x**, 0x**, 0x**, 0x**, 0x**, 0x**, 0x**, 0x**}"

where the **'s are filled in with the GUID bytes in 2-digit hex form?

def getInitializer(someUUID):
    hexByteList = [??? for b in someUUID.bytes]
    return '{'+(', '.join(hexByteList))+'}'

I'm not sure what to use for the "???" above.

+2  A: 
hex(ord(b))

...

Jason Orendorff
yay! I knew it was simple. thanks!
Jason S
(what if I wanted to ensure the string had constant length by printing byte values from 0-15 as 0x00, 0x01, etc. instead of 0x0, 0x1, etc.?)
Jason S
I think `"%#04x" % ord(b)` will do that.
Jason Orendorff