考虑以下图片:
如何将哈希码表示为二进制格式(如0110011)
谢谢
答案 0 :(得分:2)
试试这个:
Originally answered by @Ashwini Chaudhary
''.join(format(ord(n),'b') for n in hashcode)
'11001001100001101111101111100110110010110100110100110010011001011100110111000110000111011111000011001011101011100101110000111011111010111100011000101100100111000110011110101110010110011011000111100100111000'
来自@ m.wasowski的更清洁的方法,我正在结合他的想法尝试解决这个问题(仍然不确定这是否正是OP想要的,因为句子中的单词长度不一致):
def text_to_bin(text, n_bin=16):
if n_bin < 16 or n_bin > 36:
print "n_bin must be >= 16 and <= 36"
else:
for word in text.split():
word_ord = ['{}'.format(bin(int(hashlib.md5(word).hexdigest(), n_bin)))]
print '{} {}'.format(word, ''.join(word_ord)[2:])
text_to_bin('A cat in the woods', 16)
A 1111111110001010110001001110000111001111010011100001111101010000001101001011001001101011011011100101110101011001011111000101001
cat 11010000011101111111001001000100110111101111100010100111000011100101111010100111010110001011110110000011010100101111110011011000
in 10011101101011011111111101001011011110011111000101111111001000001000111001001111101100110111101001010010110000010101011011111
the 10001111110001000010110001101101110111111001100101100110110110110011101100001001111010000100001101100101000000110100001101010111
woods 10110111101111111011100101001001001001111100010000000101011101111100000100000001100111100000000010000100110101110011001010010111
答案 1 :(得分:2)
>>> import hashlib
>>> s="cat"
>>> hashcode=hashlib.md5(s).hexdigest()
>>> bin(int(hashcode,16))
'0b11010000011101111111001001000100110111101111100010100111000011100101111010100111010110001011110110000011010100101111110011011000'
答案 2 :(得分:0)
你可以使用bin()函数
bin(d077ff) # Result: '0b11...'
删除0b你可以这样做:
int(str(temp)[2:])
答案 3 :(得分:0)
有点晚了,但是尝试一下:
binhashcode = ''.join(bin(int(i,16)).zfill(4) for i in hashcode)