简体   繁体   中英

Error with dhash.get_num_bits_different

I was following this library "dhash" , but get error when using its dhash.get_num_bits_different :

def get_num_bits_different(hash1, hash2):
    return bin(hash1 ^ hash2).count('1')

hash1 = '3d77xxxxxxxxxxxxxxxxxxxxxxxxxxxx'
hash2 = '9301xxxxxxxxxxxxxxxxxxxxxxxxxxxx'

get_num_bits_different(hash1, hash2)
>>
TypeError: unsupported operand type(s) for ^: 'str' and 'str'

Convert the string to an int like:

get_num_bits_different(int(hash1, 16), int(hash2, 16))

Test Code:

def get_num_bits_different(hash1, hash2):
    return bin(hash1 ^ hash2).count('1')

hash1 = '3d77'
hash2 = '9301'

print(get_num_bits_different(int(hash1, 16), int(hash2, 16)))

Results:

10

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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