C -> PHP

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

User avatar
_ThndR
Forum Newbie
Posts: 20
Joined: Fri May 26, 2006 12:02 pm
Location: Leeuwarden, The Netherlands

C -> PHP

Post 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.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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;
}
User avatar
_ThndR
Forum Newbie
Posts: 20
Joined: Fri May 26, 2006 12:02 pm
Location: Leeuwarden, The Netherlands

Post 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...
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

does PHP version return negative values?
User avatar
_ThndR
Forum Newbie
Posts: 20
Joined: Fri May 26, 2006 12:02 pm
Location: Leeuwarden, The Netherlands

Post by _ThndR »

Nope, it returns an enourmous value (xE+40) instead of a value that is like 8 digits long.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

can you provide sample input and output?
User avatar
_ThndR
Forum Newbie
Posts: 20
Joined: Fri May 26, 2006 12:02 pm
Location: Leeuwarden, The Netherlands

Post by _ThndR »

Example

input: jorn_p90@hotmail.com
output: 1.29400903615E+40
should be: 1062137212
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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;
}
User avatar
_ThndR
Forum Newbie
Posts: 20
Joined: Fri May 26, 2006 12:02 pm
Location: Leeuwarden, The Netherlands

Post by _ThndR »

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 »

_ThndR wrote:Hmm now it returns a negative value ;): -2147483539.
Weirdan's code gave me the expected result (1062137212) in PHP 4.3.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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
User avatar
_ThndR
Forum Newbie
Posts: 20
Joined: Fri May 26, 2006 12:02 pm
Location: Leeuwarden, The Netherlands

Post 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..
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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;
}
User avatar
_ThndR
Forum Newbie
Posts: 20
Joined: Fri May 26, 2006 12:02 pm
Location: Leeuwarden, The Netherlands

Post 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?
Robert Plank
Forum Contributor
Posts: 110
Joined: Sun Dec 26, 2004 9:04 pm
Contact:

Post by Robert Plank »

_ThndR wrote:But what do these bcmath functions do exactly?
Higher decimal precision.
Post Reply