Few lines of C++ to PHP

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jenkinsp@gmail.com

    Few lines of C++ to PHP

    Hey,

    How would i do this in php ? (from borland c++)

    for (int i=1; i<=myString.Len gth(); ++i)
    {
    returnValue += StrToInt(AnsiSt ring("0x" +
    (AnsiString)myS tring[i]));
    }
    --
    thanks
    Paul


  • Hendri Kurniawan

    #2
    Re: Few lines of C++ to PHP

    I think you are trying to convert hexadecimal number to decimal, then
    add them together.
    <?php
    $temp = str_split($mySt ring);
    $returnValue = 0;
    foreach($temp as $each) $returnValue += hexdec($each);
    ?>

    Hope that helps

    Hendri Kurniawan

    jenkinsp@gmail. com wrote:
    Hey,
    >
    How would i do this in php ? (from borland c++)
    >
    for (int i=1; i<=myString.Len gth(); ++i)
    {
    returnValue += StrToInt(AnsiSt ring("0x" +
    (AnsiString)myS tring[i]));
    }
    --
    thanks
    Paul

    >

    Comment

    • Skeleton Man

      #3
      Re: Few lines of C++ to PHP

      >How would i do this in php ? (from borland c++)
      for (int i=1; i<=myString.Len gth(); ++i)
      {
      returnValue += StrToInt(AnsiSt ring("0x" +
      >(AnsiString)my String[i]));
      }
      Try something like this:

      <?
      $returnValue = 0;
      $myString = 'ABCDEF';

      for ($i=1;$i<=strle n($myString);$i ++)
      {
      $returnValue += hexdec($myStrin g[$i]);
      }

      echo $returnValue;
      ?>

      Regards,
      Chris


      Comment

      • Skeleton Man

        #4
        Re: Few lines of C++ to PHP

        Correction to my code:
        >for ($i=1;$i<=strle n($myString);$i ++)
        That should be:

        for ($i=0;$i<strlen ($myString);$i+ +)

        (I didn't realise string indexes started at 1 in C and not 0 like PHP)

        Chris



        Comment

        • jenkinsp@gmail.com

          #5
          Re: Few lines of C++ to PHP

          Thats perfect,
          thank you :)

          paul


          Skeleton Man wrote:
          Correction to my code:
          >
          for ($i=1;$i<=strle n($myString);$i ++)
          >
          That should be:
          >
          for ($i=0;$i<strlen ($myString);$i+ +)
          >
          (I didn't realise string indexes started at 1 in C and not 0 like PHP)
          >
          Chris

          Comment

          • Michael Fesser

            #6
            Re: Few lines of C++ to PHP

            ..oO(Skeleton Man)
            >for ($i=0;$i<strlen ($myString);$i+ +)
            Just a general performance note: In this case strlen() will be called
            again in each iteration, which can become an issue in a long-running
            loop or with a more complex expression.

            The value to check against should be put in a variable instead before
            entering the loop, e.g.

            for ($i = 0, $l = strlen($myStrin g); $i < $l; $i++) {
            ...
            }

            Micha

            Comment

            Working...