PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
_ThndR
Forum Newbie
Posts: 20 Joined: Fri May 26, 2006 12:02 pm
Location: Leeuwarden, The Netherlands
Post
by _ThndR » Fri Jun 23, 2006 8:19 am
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.
Weirdan
Moderator
Posts: 5978 Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine
Post
by Weirdan » Fri Jun 23, 2006 8:56 am
Code: Select all
function getUserId($user) {
$x = 0;
for($i = 0; $i < strlen($user); $i++) {
$x *= 101;
$x += ord(strtolower($user{$i}));
}
return $x;
}
_ThndR
Forum Newbie
Posts: 20 Joined: Fri May 26, 2006 12:02 pm
Location: Leeuwarden, The Netherlands
Post
by _ThndR » Fri Jun 23, 2006 9:03 am
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...
Weirdan
Moderator
Posts: 5978 Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine
Post
by Weirdan » Fri Jun 23, 2006 9:06 am
does PHP version return negative values?
_ThndR
Forum Newbie
Posts: 20 Joined: Fri May 26, 2006 12:02 pm
Location: Leeuwarden, The Netherlands
Post
by _ThndR » Fri Jun 23, 2006 9:16 am
Nope, it returns an enourmous value (xE+40) instead of a value that is like 8 digits long.
Weirdan
Moderator
Posts: 5978 Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine
Post
by Weirdan » Fri Jun 23, 2006 9:22 am
can you provide sample input and output?
_ThndR
Forum Newbie
Posts: 20 Joined: Fri May 26, 2006 12:02 pm
Location: Leeuwarden, The Netherlands
Post
by _ThndR » Fri Jun 23, 2006 9:50 am
Example
input:
jorn_p90@hotmail.com
output: 1.29400903615E+40
should be: 1062137212
Weirdan
Moderator
Posts: 5978 Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine
Post
by Weirdan » Fri Jun 23, 2006 9:55 am
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;
}
_ThndR
Forum Newbie
Posts: 20 Joined: Fri May 26, 2006 12:02 pm
Location: Leeuwarden, The Netherlands
Post
by _ThndR » Fri Jun 23, 2006 10:22 am
Hmm now it returns a negative value
: -2147483539.
Robert Plank
Forum Contributor
Posts: 110 Joined: Sun Dec 26, 2004 9:04 pm
Contact:
Post
by Robert Plank » Fri Jun 23, 2006 11:07 am
_ThndR wrote: Hmm now it returns a negative value
: -2147483539.
Weirdan's code gave me the expected result (1062137212) in PHP 4.3.
JayBird
Admin
Posts: 4524 Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:
Post
by JayBird » Fri Jun 23, 2006 11:08 am
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
_ThndR
Forum Newbie
Posts: 20 Joined: Fri May 26, 2006 12:02 pm
Location: Leeuwarden, The Netherlands
Post
by _ThndR » Fri Jun 23, 2006 12:06 pm
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..
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Fri Jun 23, 2006 12:07 pm
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;
}
_ThndR
Forum Newbie
Posts: 20 Joined: Fri May 26, 2006 12:02 pm
Location: Leeuwarden, The Netherlands
Post
by _ThndR » Fri Jun 23, 2006 12:13 pm
@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?
Robert Plank
Forum Contributor
Posts: 110 Joined: Sun Dec 26, 2004 9:04 pm
Contact:
Post
by Robert Plank » Fri Jun 23, 2006 12:19 pm
_ThndR wrote: But what do these bcmath functions do exactly?
Higher decimal precision.