13

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.

2
  • 4
    Probably me - but what are you trying to do? Commented Nov 3, 2012 at 21:13
  • Sorry, I probably didn't word it very well. I want to turn the hash into bits. I'm just not really sure how to go about doing it. Commented Nov 3, 2012 at 21:24

1 Answer 1

39

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
Sign up to request clarification or add additional context in comments.

6 Comments

I think he maybe just wants a big int ... but not sure ...(+1 all the same) either way he should be able to get his answer here
@JoranBeasley Yup - I was thinking that, and if that's the case, the OP can work with as_int... The bitstring was just an example
Wow, this takes me back. Getting an integer from a hash was the very first question I answered on SO!
@DSM wow! Well, with 20k on the clock you've come a long way since then!
Clements, you hero, you. That's exactly what I was looking for. A correct answer tick to you, sir!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.