简体   繁体   English

python中的十六进制到字符串格式转换

[英]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 character string, but once I've got 31 chars. 这种方式生成的密钥通常是32个字符的字符串,但是一旦我有31个字符。

This is what I don't get: why it's 32 chars, not 16 ? 这是我没有得到的: 为什么它是32个字符,而不是16个字符 Doesn't one hex digit take one character to print? 一个十六进制数字是否需要打印一个字符?

So if I ask for %016x - shouldn't one expect sixteen chars with possible leading zeroes? 所以如果我要求%016x - 不应该期望16个字符可能带有前导零吗?

Why string legth is not always the same? 为什么字符串legth并不总是一样?

Test case 测试用例

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

for key in stats:
    print key, ' ', stats[key]

Prints: 打印:

32   937911
27   1
28   9
29   221
30   3735
31   58123

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. 是的,但你正在使用的格式没有截断 - 你生成128个随机位,这需要(通常)32个十六进制数字来显示,而%016意味着至少16个十六进制数字,但不只是扔掉你需要额外的那些显示所有128位数字。 Why not generate just 64 random bits if that's what you actually need? 如果那是你真正需要的,为什么不生成64个随机位? 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; 为了满足您的好奇心,长度偶尔为31位,因为16位中的1位时间前4位将全部为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. 实际上在256中有1次,所有前8位都是0,所以你只能获得30位数等等。你只需要16位数字,所以格式化的数量最少> = 16且不需要你没有要求的截断。

Each hex characters from 0 to F contains 4 bits of information, or half a byte. 从0到F的每个十六进制字符包含4位信息或半字节。 128 bits is 16 bytes, and since it takes two hex characters to print a byte you get 32 characters. 128位是16个字节,因为它需要两个十六进制字符来打印一个字节,所以你得到32个字符。 Your format string should thus be '%032x' which will always generate a 32-character string, never shorter. 因此,您的格式字符串应为'%032x' ,它将始终生成一个32个字符的字符串,而不会更短。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM