Skip to content Skip to sidebar Skip to footer

Hex To String Formatting Conversion In Python

I used to generate random string in the following way (now I've switched to this method). key = '%016x' % random.getrandbits(128) The key generated this way is most often a 32 cha

Solution 1:

Yes, but the format you're using doesn't truncate -- you generate 128 random bits, which require (usually) 32 hex digits to show, and the %016 means AT LEAST 16 hex digits, but doesn't just throw away the extra ones you need to show all of that 128-bit number. Why not generate just 64 random bits if that's what you actually need? Less work for the random generator AND no formatting problems.

To satisfy your side curiosity, the length is occasionally 31 digits because 1 time in 16 the top 4 bits will all be 0; actually 1 time in 256 all the top 8 bits will be 0 so you'll get only 30 digits, etc. You've only asked for 16 digits, so the formatting will give the least number that's >= 16 and doesn't require the truncation you have not asked for.


Solution 2:

Each hex characters from 0 to F contains 4 bits of information, or half a byte. 128 bits is 16 bytes, and since it takes two hex characters to print a byte you get 32 characters. Your format string should thus be '%032x' which will always generate a 32-character string, never shorter.

jkugelman$ cat rand.py
#!/usr/bin/env python

import random
import collections
stats = collections.defaultdict(int)
for i in range(1000000):
    key = '%032x' % random.getrandbits(128)
    length = len(key)
    stats[length] += 1

for key in stats:
    print key, ' ', stats[key]
jkugelman$ python rand.py
32   1000000

Post a Comment for "Hex To String Formatting Conversion In Python"