Page 1 of 2
C -> PHP
Posted: Fri Jun 23, 2006 8:19 am
by _ThndR
Yup, me again.
I'm trying to translate a little piece of C to PHP, but I don't seem to do it right. Here's the code to translate:
Code: Select all
int getUserId(LPTSTR user)
{
unsigned int x = 0;
for (int i = 0; i < strlen(user); i++) {
x = x * 101;
x = x + towlower(user[i]);
}
return x;
}
(Source:
http://www.fanatic.net.nz/2003/09/18/ho ... er-id.html)
I think the thing is that PHP doesn't support unsigned data types... Does someone know how to correctly translate this to PHP?
Thanks in advance.
Posted: Fri Jun 23, 2006 8:56 am
by Weirdan
Code: Select all
function getUserId($user) {
$x = 0;
for($i = 0; $i < strlen($user); $i++) {
$x *= 101;
$x += ord(strtolower($user{$i}));
}
return $x;
}
Posted: Fri Jun 23, 2006 9:03 am
by _ThndR
Hmm I already tried practically the same, but doesn't give the correct value though...
Thanks for posting though
I think it has got something to do with the uint data type that can't be used in php...
Posted: Fri Jun 23, 2006 9:06 am
by Weirdan
does PHP version return negative values?
Posted: Fri Jun 23, 2006 9:16 am
by _ThndR
Nope, it returns an enourmous value (xE+40) instead of a value that is like 8 digits long.
Posted: Fri Jun 23, 2006 9:22 am
by Weirdan
can you provide sample input and output?
Posted: Fri Jun 23, 2006 9:50 am
by _ThndR
Example
input:
jorn_p90@hotmail.com
output: 1.29400903615E+40
should be: 1062137212
Posted: Fri Jun 23, 2006 9:55 am
by Weirdan
Code: Select all
function getUserId($user) {
$x = 0;
for($i = 0; $i < strlen($user); $i++) {
$x = intval($x * 101);
$x += ord(strtolower($user{$i}));
}
return $x;
}
Posted: Fri Jun 23, 2006 10:22 am
by _ThndR
Hmm now it returns a negative value

: -2147483539.
Posted: Fri Jun 23, 2006 11:07 am
by Robert Plank
_ThndR wrote:Hmm now it returns a negative value

: -2147483539.
Weirdan's code gave me the expected result (1062137212) in PHP 4.3.
Posted: Fri Jun 23, 2006 11:08 am
by JayBird
Robert Plank wrote:_ThndR wrote:Hmm now it returns a negative value

: -2147483539.
Weirdan's code gave me the expected result (1062137212) in PHP 4.3.
Same here on PHP 4.3.10
Posted: Fri Jun 23, 2006 12:06 pm
by _ThndR
That's uhm.. Rather strange. I've got PHP 4.4.2 here, and just copied Weirdan's code, but it's still giving a negative value..
Posted: Fri Jun 23, 2006 12:07 pm
by feyd
The differing results are likely due to integer storage differences across platforms. I ran into this issue when making SHA256. So this should work across platforms provided bcmath is enabled.
Code: Select all
function getUserId($str)
{
$x = '0';
$user = strval($str);
$j = strlen($user);
$keep = bcpow('2', '32', 0);
for ($i = 0; $i < $j; ++$i)
{
$x = bcmul($x, '101', 0);
$x = bcmod($x, $keep, 0);
$x = bcadd($x, ord(strtolower($user[$i])), 0);
}
return $x;
}
Posted: Fri Jun 23, 2006 12:13 pm
by _ThndR
@feyd: Wow that did the trick! I did have to remove the '0' from the bcmod function, because it only seemed to take two arguments. Thanks a lot!
But what do these bcmath functions do exactly?
Posted: Fri Jun 23, 2006 12:19 pm
by Robert Plank
_ThndR wrote:But what do these bcmath functions do exactly?
Higher decimal precision.