Showing posts with label format strings. Show all posts
Showing posts with label format strings. Show all posts

Monday, October 1, 2012

Python convert to hex

In Python, the format string for printing 2-digit hex is %02x or %02X. You store binary data in strings, and print the ordinal of the array index of the string in question.

This forum thread has one low-noise response that gives good examples:

http://www.velocityreviews.com/forums/t350972-hexadecimal-conversion-in-python.html

Here is another very nice set of answers to the question in which the bytes from a "string" are being printed after being read in from a file. The keys in the following are using the hex(), using the ord() of the character, and alternate examples such as decode() and int(,16):

http://code.activestate.com/recipes/496969-convert-string-to-hex/