LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-12-2008, 02:02 PM   #1
hbar
Member
 
Registered: Dec 2007
Location: Canada
Distribution: Debian
Posts: 50

Rep: Reputation: 15
PHP: Map string to integer


I'm looking for a function to map a string to a number in the range [0,MAX]. What the number actually is doesn't really matter, but it should be repeatable for a given input (not random).

I've had a look around and can't find anything on this. I could write a function myself but I know it would be horribly inefficient and there's probably a perfect solution floating around somewhere...
 
Old 05-12-2008, 03:25 PM   #2
AdaHacker
Member
 
Registered: Oct 2001
Location: Brockport, NY
Distribution: Kubuntu
Posts: 384

Rep: Reputation: 32
Quote:
Originally Posted by hbar View Post
I'm looking for a function to map a string to a number in the range [0,MAX]. What the number actually is doesn't really matter, but it should be repeatable for a given input (not random).
If the number really doesn't matter, then this should work.
Code:
function map_to_int($some_string) { return 1; }
But seriously, how about some more information. Maybe tell us what it is you're trying to accomplish? What your range of ints is? What kind of strings you're going to be getting? It's hard to give a useful answer without any sort of context.
 
Old 05-12-2008, 05:46 PM   #3
hbar
Member
 
Registered: Dec 2007
Location: Canada
Distribution: Debian
Posts: 50

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by AdaHacker View Post
Code:
function map_to_int($some_string) { return 1; }
A har har har, didn't know we had a comedian in the house

I have an array of words or phrases, and given an input word (string), I need to associate it with one of the elements in the array (pseudo-randomly, but a given input should always produce the same result). So if the array has a size MAX, then I need to produce numbers in the range [0, MAX-1].

For the size of MAX, say, 100.

I basically need a fast hash function that produces an integer in a given range.
 
Old 05-12-2008, 06:59 PM   #4
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
PHP Code:
$hash md5 ($yourstring) % (MAX-1); 
Not sure whether you should use MAX or MAX-1, too lazy to think now, I leave that up to you.

jlinkels
 
Old 05-12-2008, 09:39 PM   #5
AdaHacker
Member
 
Registered: Oct 2001
Location: Brockport, NY
Distribution: Kubuntu
Posts: 384

Rep: Reputation: 32
Quote:
Originally Posted by jlinkels View Post
PHP Code:
$hash md5 ($yourstring) % (MAX-1); 
Actually, that won't work. The md5() function returns a string, not an integer. Granted, it's a hexidecimal string, but when you do the modulus, PHP casts it to an int, which truncates everything after the first non-digit character. So any string for which the MD5 sum starts with a letter will get mapped to 0.

Edit:
Actually, on second thought, that may be close enough for current purposes, as it will certainly generate consistent values. It's just that the results won't be consistent with the actual numeric value of the MD5 sum. But that may not be a problem in this case.

Last edited by AdaHacker; 05-12-2008 at 10:05 PM.
 
Old 05-12-2008, 10:00 PM   #6
AdaHacker
Member
 
Registered: Oct 2001
Location: Brockport, NY
Distribution: Kubuntu
Posts: 384

Rep: Reputation: 32
Quote:
Originally Posted by hbar View Post
A har har har, didn't know we had a comedian in the house
Yeah, sorry, but I have a penchant for bad jokes and you left yourself wide open on that one.

Quote:
I basically need a fast hash function that produces an integer in a given range.
Well, I'm still not real clear on what you're actually trying to accomplish, but at least now we've got some context.

The easiest solution is probably the approach jlinkels suggested -using a pre-defined hash function and a modulus. The only problem is that you need a hash function that will generate an integer - at 128-bits and 160-bits respectively, both MD5 and SHA1 generate values far to large to fit into an integer data type. Your best bet is probably CRC32, as it actually generates a 32-bit integer value. You'll just have to convert the return value to an unsigned int as described in the documentation.
 
Old 05-14-2008, 09:43 AM   #7
hbar
Member
 
Registered: Dec 2007
Location: Canada
Distribution: Debian
Posts: 50

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by AdaHacker View Post
Your best bet is probably CRC32, as it actually generates a 32-bit integer value. You'll just have to convert the return value to an unsigned int as described in the documentation.
This will do the trick. Thanks everyone
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
c convert integer to string? kpachopoulos Programming 5 08-23-2006 11:57 AM
C++ string to integer conversion fatman Programming 6 06-18-2006 08:17 AM
C how to convert an integer to string? totti10 Programming 10 11-10-2005 05:49 AM
Integer To a String in C Bean101 Programming 2 05-27-2004 04:46 AM
How do you convert a string to an integer? wbdune Linux - Newbie 11 11-01-2003 02:01 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:32 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration