md5 to integer bits in python

2019-06-24 07:25发布

I'm trying to convert an MD5 hashed value into a a bit integer in python. Does anyone have any idea how I would go about doing this?

I currently go through several ngrams applying a hash to each ngram:

for sentence in range(0,len(doc)):
        for i in range(len(doc[sentence]) - 4 + 1):
            ngram = doc[sentence][i:i + 4]
            hashWord = hashlib.md5()
            hashWord.update(ngram)

Thanks for any help.

标签: python hash
1条回答
▲ chillily
2楼-- · 2019-06-24 08:13

If by "into bits", you mean a bit string for instance, then something like:

import hashlib

a = hashlib.md5('alsdkfjasldfjkasdlf')
b = a.hexdigest()
as_int = int(b, 16)
print bin(as_int)[2:]
# 11110000110010001100111010111001011010101011110001010000011010010010100111100
查看更多
登录 后发表回答