Converting a string to a number by using INT (no hash method)

Michael Torrie torriem at gmail.com
Tue Jan 22 14:02:48 EST 2013


On 01/22/2013 11:37 AM, Ferrous Cranus wrote:
> ============================================== pin = int(
> htmlpage.encode("hex"), 16 ) % 10000 
> ==============================================
> 
> Can you please explain the differences to what you have posted
> opposed to this perl coding?
> 
> ============================================== foreach my
> $ltr(@ltrs){ $hash = ( $hash + ord($ltr)) %10000; 
> ==============================================
> 
> I want to understand this and see it implemented in Python.

It isn't quite the thing.  The perl code is merely a checksum of the
ascii value of the characters in the file name, that is then chopped
down to a number < 10000.  The Python code is taking the ascii value of
each character in the file name, converting it to a hexadecimal pair of
digits, stringing them all out into a long string, then converting that
to a number using the hexadecimal number parser. This results in a
*very* large number, 8-bits per letter in the original file name, and
then chops that down to 10000.  Technically neither method is a hash and
neither will generate unique numbers.

Here's the python algorithm used on a short word:
'hello' => '68656c6c6f' (h = 0x68', e=0x65', 0x6c', 0=0x6f)
=> 0x68656c6c6f => 448378203247
mod that with 10000 and you get 3247

If you would simply run the python interpreter and try these things out
you could see how and why they work or not work.  What is stopping you
from doing this?



More information about the Python-list mailing list