简体   繁体   English

使用Python对字节字符串进行密集的人类可读表示

[英]Dense, human-readable representation of a string of bytes using Python

I've got a bunch of bytes I want to output in a human-friendly fashion (using characters that will be available and printable in any font/encoding/etc.). 我有一堆想要以人类友好的方式输出的字节(使用将以任何字体/编码/等形式提供和打印的字符)。 In my case, the bytes are the result of an md5 sum: 在我的情况下,字节是md5总和的结果:

import hashlib
h = hashlib.md5("foo")

The HASH object has two ways of displaying its contents to me. HASH对象有两种向我显示其内容的方法。

print h.digest() # Uses a bunch of unprintable characters
print h.hexdigest() # Readable, but 32 characters long

The second option gives me a well-behaved string of characters that I can read, cut and paste, or whatever. 第二个选项为我提供了行为良好的字符串,可以读取,剪切和粘贴等等。 But it's an inefficient representation: it only uses 16 characters because it's hexadecimal. 但这是一种低效的表示形式:它仅使用16个字符,因为它是十六进制的。 It could give me a shorter string if it used the whole alphabet, uppercase letters, punctuation, etc. Can I get a shorter, denser digest by expanding beyond hex? 如果它使用整个字母,大写字母,标点符号等,它可能会给我一个较短的字符串。通过扩展到十六进制之外,是否可以获得较短,更密集的摘要?

Here's a modified version of one of the answers to the question that @vaultah linked to: 这是@vaultah链接到的问题的答案之一的修改版:

import hashlib, string, base64

_INT_EFFICIENT_CHARS = string.letters + string.digits + string.punctuation
_L_INT_EFFICIENT_CHARS = len(_INT_EFFICIENT_CHARS)
# http://stackoverflow.com/questions/2267362/convert-integer-to-a-string-in-a-given-numeric-base-in-python
def int_efficient(x):
    rets=''
    while x>0:
        x,idx = divmod(x, _L_INT_EFFICIENT_CHARS)
        rets = _INT_EFFICIENT_CHARS[idx] + rets
    return rets

h = hashlib.md5("foo")
print h.hexdigest()

# Starting in Python 3.2, use int.from_bytes rather than converting to hex
# http://stackoverflow.com/a/9634417/2829764
i = int(h.hexdigest(), 16)
print int_efficient(i)

print base64.b64encode(h.digest())

Using my alphabet (94 characters) only shortens the result by a few characters relative to base64: 使用我的字母(94个字符)只会使结果相对于base64缩短几个字符:

acbd18db4cc2f85cedef654fccc4a4d8
Hpf=RjPL{_{4Q-[X$vdO
rL0Y20zC+Fzt72VPzMSk2A==

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

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