#### Archived

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

# generating a unique number from 2 strings

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

## 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 on other sites
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 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 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 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 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 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 on other sites
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

##### 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.

1. 1
Rutin
32
2. 2
3. 3
4. 4
5. 5

• 13
• 57
• 11
• 10
• 14
• ### Forum Statistics

• Total Topics
632967
• Total Posts
3009557
• ### Who's Online (See full list)

There are no registered users currently online

×