Thread: Need help converting a C function to PHP

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    19

    Need help converting a C function to PHP

    Hello,

    I need some help converting a C function to PHP. I know what it can do, but I can't seem to think of how to write the function in PHP. Here is the function in C:
    Code:
    u32 setver(u8 *gamever) {
        int     v1  = 0,
                v2  = 0,
                v3  = 0,
                v4  = 0;
    
        sscanf(gamever, "%d.%d.%d.%d", &v1, &v2, &v3, &v4);
        return(((v1 & 0xf) << 28) | ((v2 & 0xf) << 24) | ((v3 & 0xffff) << 8) | (v4 & 0xff));
    }
    The function is supposed to convert a game's version number into a 32bit hex string. So a sample game version is 1.1.2965-797.0, and you have in it n1.n2.n3.n4. And this string correctly converted to its 32bit hex form should be 110b9500 (1.1.0b95.00).

    On n3, you have 0b95 which is 2965 in hex, so I think the function is written so the -797 is ignored for that specific game version, but I'm not quite sure.

    Probably pretty simple for you guys . I thought I would try here first to see if anyone knows, and if not I can try a php forum too. Thanks!

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    I don't understand how this is related to C? You're going from C to PHP not from PHP to C.

    A search of php.net for 'bitwise' came up with: http://php.net/manual/en/language.operators.bitwise.php

    * sscanf() exists in PHP, http://php.net/manual/en/function.sscanf.php

    So what's the problem?

    Code:
    function setver($gamever)
    {
        $v = array(0, 0, 0, 0);
        sscanf($gamever, "&#37;d.%d.%d.%d", $v[0], $v[1], $v[2], $v[3]);
    
    // ...
    Is how I'd start it, dunno been a while since I've done any PHP.
    Last edited by zacs7; 10-04-2007 at 01:21 AM.

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    19
    Quote Originally Posted by zacs7 View Post
    I don't understand how this is related to C? You're going from C to PHP not from PHP to C.
    Yea, probably a bad choice for a forum, sorry. I'll probably head over to phpfreaks or something and ask if I can't get it from this point.

    However your code looks like a good start, and I can play with the return line and see if I can get the result I'm looking for. Thanks for the help

  4. #4
    Registered User
    Join Date
    Aug 2006
    Posts
    19
    Alright thanks, your code helped! I ended up getting the rest:

    Code:
    function setver($gamever)
    {
    	$v = array(0, 0, 0, 0);
    	sscanf($gamever, "%d.%d.%d.%d", $v[0], $v[1], $v[2], $v[3]);
    	$decimal = (($v[0] & 0xf) << 28) | (($v[1] & 0xf) << 24) | (($v[2] & 0xffff) << 8) | ($v[3] & 0xff);
    	return(dechex($decimal));
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  3. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM