Convert 4-byte string to integer

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dave Turner

    #1

    Convert 4-byte string to integer

    Ive used fwrite to store 4 bytes in a file: "Test" (or 0x54657374 /
    1953719636). Its easy enough to use fread to get those 4 bytes, but the
    problem is I need to work with the data as a number and not a string, so how
    can I convert it to the number 1953719636 ?


  • Ewoud Dronkert

    #2
    Re: Convert 4-byte string to integer

    "Dave Turner" <nobody@nowhere .nohow> wrote in message
    news:426f6ca7$1 @quokka.wn.com. au...[color=blue]
    > Ive used fwrite to store 4 bytes in a file: "Test" (or 0x54657374 /
    > 1953719636). Its easy enough to use fread to get those 4 bytes, but the
    > problem is I need to work with the data as a number and not a string, so
    > how can I convert it to the number 1953719636 ?[/color]




    Comment

    • Dave Turner

      #3
      Re: Convert 4-byte string to integer

      Example please? It doesnt seem to be working ...

      <?
      $packed = "Test";
      $unpacked = unpack("L", $packed);
      echo $unpacked;
      ?>

      It should echo 1953719636, but it's not displaying anything



      Comment

      • Dave Turner

        #4
        Re: Convert 4-byte string to integer

        Hmmm, ok :-) Well this works, but it doesnt seem very efficient:

        <?
        $Buffer = "Test";

        $Number = ord($Buffer{0}) | (ord($Buffer{1} )<<8) | (ord($Buffer{2} )<<16) |
        (ord($Buffer{3} )<<24);

        echo $Number;
        ?>

        If anyone knows of a better method I'd appreciate it, thanks


        Comment

        • Ewoud Dronkert

          #5
          Re: Convert 4-byte string to integer

          "Dave Turner" <nobody@nowhere .nohow> wrote:[color=blue]
          > Example please? It doesnt seem to be working ...
          >
          > <?
          > $packed = "Test";
          > $unpacked = unpack("L", $packed);
          > echo $unpacked;
          > ?>
          >
          > It should echo 1953719636, but it's not displaying anything[/color]

          It does work, but you didn't read the docpage well. unpack() returns an
          array so use print_r() instead of echo.

          E.


          Comment

          • Dave Turner

            #6
            Re: Convert 4-byte string to integer

            > It does work, but you didn't read the docpage well. unpack() returns an[color=blue]
            > array so use print_r() instead of echo.[/color]
            Thankyou for clarifying that, "echo $unpacked[1];" also works

            So i guess my final question is - what is more efficient? unpack() or 4 x
            Ord() ?


            Comment

            • Ewoud Dronkert

              #7
              Re: Convert 4-byte string to integer

              "Dave Turner" <nobody@nowhere .nohow> wrote:[color=blue]
              > So i guess my final question is - what is more efficient? unpack() or 4 x
              > Ord() ?[/color]

              function utime()
              {
              list( $usec, $sec ) = explode( ' ', microtime() );
              return ((float)$usec + (float)$sec);
              }

              $t = utime();
              for ( $i = 0; $i < 1000000; ++$i )
              {
              //insert code for testing here
              }
              $t = utime() - $t;
              echo "Time : $t sec\n";


              Comment

              • Dave Turner

                #8
                Re: Convert 4-byte string to integer

                Very useful timing routine, thanks. The unpack/array[1] method is faster by
                about 25% over the 4 x ord() method


                Comment

                Working...