Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

generating a unique number from 2 strings

This topic is 5581 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!