php - Converting string to an integer between a particular range -


i have alphanumeric string values. need represent each string integer value.

resultant integers must between 30 , 150.
multiple strings can have same integer value.
specific string must give same integer value in every run.
crypto not important, string values not valuable.
faster calculation preferred.
integer musn't have same value.

edit: tried intval(), gave me zero. tried md5 , sha1 gave me long strings. tried base_convert couldn't make it.

this require hash function. range 150-30=120 you'll need modulo 120 (% 120) result , add 30.

in php standard hash functions (md5, sha) security purposes , return string aren't useful. in case use cyclic redundancy check: http://php.net/crc32. isn't hash (values aren't distributed evenly) close enough purposes.

$stringhash = crc32($string) % 120 + 30; 

if need more distribution can user simple standard hash, e.g. md5, slower.

// take first 8 alphanumeric digits of hash (= 32 bits) because want keep int , not convert floating point $stringhash = intval(substr(md5($string), 0, 8), 16) % 120 + 30; 

Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -