www.webdeveloper.com
Results 1 to 11 of 11

Thread: Converting old Delphi way to PHP

  1. #1
    Join Date
    Apr 2008
    Posts
    16

    Converting old Delphi way to PHP

    Hey,

    I'm working on something for which I have a function prewritten in Delphi from the company I'm working with. Delphi is a language I don't know so I'm trying to convert it to PHP.

    Here you can see the Delphi function:

    PHP Code:
    function GetNumber(AnInputstring): string;
    var
      
    iInteger;
      
    MyLRCByte;
      
    sstring;
    begin
      Result 
    := '';
      if 
    Length(AnInput) = 0 then
        
    Exit;
      
    MyLRC := Ord(AnInput[1]);

      for 
    := 1 to Length(AnInput) do
      
    begin
        MyLRC 
    := MyLRC xor Ord(AnInput[i]);
      
    end;

      
    := IntToStr(MyLRC);
      while 
    Length(s) < do
        
    := '0' s;
      
    Result := s;
    end
    What it does is generate a 3 digit number based on five other variables, which are placed in a string. Here you can see my attempt to convert it to PHP:
    PHP Code:
    function GetNumber($string){
        
        if (
    strlen($string)==0){ 
                exit;
        }
            
    $MyLRC=ord($string[1]);
            for(
    $i=1;$i<(strlen($string)); $i++){
                
    $MyLRC=$MyLRC xor $MyLRC=ord($string[$i]);
            }
            
    $x=strval($MyLRC);
            while (
    strlen($x)<3){
                
    $x='0'.$x;
            
            return 
    $x;
        }

    The rest of the code looks like this (mind you this is just a test to see if the function can generate the right url with the right number on the end.

    PHP Code:
    $date = (string)date('ymd');
    $time=  (string)date('His');

    //hex current date in yymmdd + 0 on the start
    $s='0'.dechex($date);
    $s=strtoupper($s);
    //hex current date in hhmmss + 0 on the start
    $t='0'.dechex($time);
    $t=strtoupper($t);

    //specific code 12 characters long
    $u '234323434321';
    //usernumber test
    $v='00067E';
    //specific code w 12 long
    $w '234323434321';
    //get number 3 long 
    $x GetNumber($s.$t.$u.$v.$w);
    //string together above variables
    $id=$s.$t.$u.$v.$w.$x;

    //total url
    $url="http://website.com/XML.dll?id=".$id;


    //echo url for testing purpose
    echo $url."<br/>";

    echo 
    "<a href=\"".$url."\">Click here to test the url</a>"

    The whole file does (or should do) basically this: It gives a hex value of the current date and time in uppercases. Form a string out of this with the set variables ($s.$t.$u.$v.$w). This string should be used by the function to generate a unique number with every change (at least that is what I understood out of it). This unique number is max. 3 characters long and when it is less, it adds a '0' in front of it. However, I always seem to get 050 or 049 as a result, depending on where I put the brackets inside the function.

    I hope someone can help, thank you in advance.

    Best regard,

    Marcus

    BTW If you need some more information, please feel free to ask
    Last edited by Marcus22; 01-03-2011 at 11:09 AM.

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,147
    I've not tried to figure out the whole thing, but I suspect you may need to make your string index zero-based instead of one-based:
    PHP Code:
    for($i 0$i strlen($string); $i++) { 
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Apr 2008
    Posts
    16
    Thank you, however I've tried, and it does not seem to help. It only decreases the number by 1 to 048 instead of 049.
    I think there is a mistake somewhere in the function, maybe with the xor statement, but I don't seem to understand it.
    Last edited by Marcus22; 01-03-2011 at 11:25 AM.

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,147
    I'm guessing that this:
    Code:
    MyLRC := MyLRC xor Ord(AnInput[i]);
    ...should translate as:
    PHP Code:
    $MyLRC=($MyLRC xor ord($string[$i])); 
    The extra parens around the whole expression are needed because in PHP the "xor" operator has a lower precedence than the assignment operators.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Apr 2008
    Posts
    16
    This would leave the resulting string empty it seems?
    PHP Code:
    function GetNumber($string){
        
            if (
    strlen($string)==0){ 
                exit;
        }
        
    $MyLRC=(ord($string[1]));
        
    $strlength strlen($string);
        for(
    $i=0;$i<$strlength$i++){
            
    $MyLRC=($MyLRC xor $MyLRC=ord($string[$i]));
        }
            
    $s=strval($MyLRC);
            while ((
    strlen($s))<3){
                
    $s='0'.$s;
            
            return 
    $s;
            
            
        }

    Last edited by Marcus22; 01-03-2011 at 12:51 PM. Reason: small improvement to function, still doesn't work

  6. #6
    Join Date
    Apr 2008
    Posts
    16
    PHP Code:
    function GetNumber($string){
        if ((
    strlen($string[1]))==0){
            echo 
    "string length is equal to zero";
            exit;
        }
        else{
             
    $MyLRC=(ord($string[1]));
            
    $strlength strlen($string);
              for (
    $i=0;$i<$strlength$i++){
                
    $MyLRC=($MyLRC xor $MyLRC=ord($string[$i]));
              }

              
    $s=strval($MyLRC);
              
    $strlength strlen($s);
              while (
    $strlength 3){ 
                
    $s '0' $s;
                return 
    $s;
              }
        }

    Another small improvement to the function. Just a thought: Maybe the 'ord' and/or xor function(s) is(are) different in PHP compared to Delphi? It now only outputs a zero.
    Last edited by Marcus22; 01-03-2011 at 01:24 PM.

  7. #7
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,147
    Try the PHP ^ "bitwise exclusive or" operator -- probably makes more sense anyway when I think about it.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  8. #8
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,147
    PS: I looks like ord() is the same in Delphi: http://www.delphibasics.co.uk/RTL.as...xpandCode1=Yes
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  9. #9
    Join Date
    Apr 2008
    Posts
    16
    Thank you again, but it's still the same. BTW thank you for taking the time to help me, I really really appreciate it.

    I'm now trying to go through the steps to see if it makes sense and if it does as it is supposed to.

    Date(yymmdd) is turned in to hex code with dechex(), returns five digits, six digits are needed so a zero is added in front. The same goes for date('His').

    Then this together with 3 other values is turned into a string with the total value of '013317031A53300406C5348600067E086097110032' (for a certain time). This is
    inserted in the function.

    PHP Code:
    function GetNumber($string){
        
        
        if ((
    strlen($string))==0){
            echo 
    "string length is equal to zero";
            exit;
        }
        else{
             
    $MyLRC=(ord($string{1}));
            echo 
    $MyLRC;
            
    $strlength strlen($string);
              for (
    $i=0;$i<$strlength$i++){
                
    $MyLRC=$MyLRC $MyLRC=ord($string[$i]);
              

              
    $s=strval($MyLRC);
             while (
    strlen($s) < 3){ 
                
    $s '0' $s;
                
              return 
    $s;}
        }}

    The function first checks the stringlength not to be zero and shows error message otherwise // well this seems to be ok.

    Second, the function defines $MyLRS as (ord($string{1}))// echo'ing this results in '49'. Changing the number between the brackets changes the value.

    Thirdly this happens:
    PHP Code:
    $strlength strlen($string);
              for (
    $i=0;$i<$strlength$i++){
                
    $MyLRC=($MyLRC $MyLRC=ord($string[$i]));
              } 
    I don't seem to really understand anymore what this does. For each string($i), up untill the $i=strlen(string), it check if both $MyLRC=$MyLRC and $MyLRC=ord(($string[$i])) is true? Doesn't that say then (ord($string{1}))=ord($string[$i]) and thus [$i]=[1]? Maybe I completely misunderstand its purpose, I have never worked with it before. // echo'ing this gives '0', so I think here is the problem with the function.

    Thereafter, $s=strval($MyLRC), converts the $MyLRC into a string (which I think it already is, isn't it?) and while this strings length is <3 (which it is right now as it gives a '0' value, it adds a zero. Then finally it returns this value.

    So I think the problem is in this part
    PHP Code:
    for ($i=1;$i<$strlength$i++){
                
    $MyLRC=$MyLRC $MyLRC=ord($string[$i]) 
    Judging from the instructions I was given, this is generating a unique code everytime in the Delphi function(as the time changes, so does the total value of the string), while it is now returning '0' in the PHP function.
    Last edited by Marcus22; 01-03-2011 at 02:24 PM.

  10. #10
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,147
    My best guess:
    PHP Code:
    function GetNumber($string)
    {
       if ((
    strlen($string)) == 0) {
          echo 
    "string length is equal to zero";
          exit;
       } else {
          
    $MyLRC = (ord($string[0])); // start with 0
          // echo $MyLRC;
          
    $strlength strlen($string);
          for (
    $i 0$i $strlength$i++) {
             
    // $MyLRC = $MyLRC ^ $MyLRC = ord($string[$i]);
             // try this instead without the second assignment:
             
    $MyLRC $MyLRC ord($string[$i]);
             
    $s strval($MyLRC);
          }
          
    // move this outside of the for loop:
          
    while (strlen($s) < 3) {
             
    $s '0' $s// concatenate, don't add
          
    }
       }
       return 
    $s;
    }

    $test '013317031A53300406C5348600067E086097110032';
    $result GetNumber($test);
    var_dump($result); // "073" 
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  11. #11
    Join Date
    Apr 2008
    Posts
    16
    Seriously I can not thank you enough, that did it! I can now move on with the project. Again thank you for the time you wanted to put into it, it is great to see people are still willing to do that for each other. If you ever need help, let me know.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles