generating a unique number from 2 strings

Started by
7 comments, last by billybob 21 years, 2 months ago
i need to get a unique 32 bit number for every file in my game, and i was wondering how i could make a generator with the following charactaristics: -generates a different number from two strings -the same two strings always generate the same number -different strings never generate the same number (this doesn''t have to be 100%, but the more unique, the better) i''ve been racking my brain for a while now and i have NO idea how i woudl do this
Advertisement
Would the above really work? Since you''re working on chars (ie bytes) here, the shifts would basically kill every letter, it seems atleast, even though i haven''t tried it...

Generating a unique number from two strings would probably be easiest by using a hash. Say for example: key MOD nnnn
where nnnn is a fairly large prime. What is "key" you ask?
Well, concatenate the 2 strings into 1, and then just add the characters together to make a (fairly large) number you can use as key.

This is written pretty much from the top of my head, and im not 100% sure i remember it correctly, but just google for hashing or something like that, and you''ll have a better explanation



--
MFC is sorta like the swedish police... It''''s full of crap, and nothing can communicate with anything else.
Oh, or you could just calculate the CRC-32 checksum for the filename... Check google for a description, it''s pretty easy


--
MFC is sorta like the swedish police... It''''s full of crap, and nothing can communicate with anything else.
quote:Original post by tok_junior
Would the above really work? Since you're working on chars (ie bytes) here, the shifts would basically kill every letter, it seems atleast, even though i haven't tried it...

would casting them to unsigned longs work? that would make it so it had 'room' to shift:

result1 += (DWORD)source1[j] << 24 | (DWORD)source1[j-1] << 16 | (DWORD)source1[j-2] << 8 | source1[j-3];    result2 += (DWORD)source2[j] << 24 | (DWORD)source2[j-1] << 16 | (DWORD)source2[j-2] << 8 | source2[j-3];  



[edited by - billybob on February 8, 2003 8:30:02 PM]

[edited by - billybob on February 8, 2003 8:30:40 PM]
The problem with just checking the first four chars is that it won''t work for similar filenames e.g. hello1 and hello2 would be the same!

The best thing to do is a CRC32 as suggested by another poster.
The problem with just checking the first four chars is that it won''t work for similar filenames e.g. hello1 and hello2 would be the same!

The best thing to do is a CRC32 as suggested by another poster.
it loops until the end so it reads all the chars, but it doesn''t matter, i''m reading about CRC-32 now. the one i''ve found i''m kind of doubting, since it seems a little too simple.
MD5 is better than CRC, but its not 4 bytes =-) You can find full implementations of it using a google search. There is a pdf about it that contains full source somewhere on http://www.rsasecurity.com
"Walk not the trodden path, for it has borne it's burden." -John, Flying Monk
i have read about md5, and i''m using that for my archive files, but its too big/slow to use on every file in the archives. the md5 is for verifying the authenticity of the archive, but the little checksum on each file is for resource management, so i dont load stuff more than once.

This topic is closed to new replies.

Advertisement