Consider the following image:
How can I represent hashcode to binary format (as 0110011)
Thank you
Consider the following image:
How can I represent hashcode to binary format (as 0110011)
Thank you
>>> import hashlib
>>> s="cat"
>>> hashcode=hashlib.md5(s).hexdigest()
>>> bin(int(hashcode,16))
'0b11010000011101111111001001000100110111101111100010100111000011100101111010100111010110001011110110000011010100101111110011011000'
Try this:
Originally answered by @Ashwini Chaudhary
''.join(format(ord(n),'b') for n in hashcode)
'11001001100001101111101111100110110010110100110100110010011001011100110111000110000111011111000011001011101011100101110000111011111010111100011000101100100111000110011110101110010110011011000111100100111000'
Much cleaner approach from @m.wasowski, I am combining his idea to try and solve this (still not sure if this is what exactly OP wants, since lengths of words in sentence are not consistent):
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
you can use the bin() function
bin(d077ff) # Result: '0b11...'
to remove 0b you can do this:
int(str(temp)[2:])
A little late but try this:
binhashcode = ''.join(bin(int(i,16)).zfill(4) for i in hashcode)