atoi and similar functions?

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

    #1

    atoi and similar functions?

    Could someone please let me know the equivalent of C atoi and similar
    functions? I need to convert a hex text string - e.g. '0x01' - to its
    integer equivalent.

    I also want to be able to print the contents of a binary string as its
    integer values:

    $x = pack("n",0x20);
    print $x;

    prints one space, as 0x20 is the ascii representation for a ' '.

    My C background really blinds me sometimes....

    --Yan
  • Andy Hassall

    #2
    Re: atoi and similar functions?

    On Mon, 08 Jan 2007 09:46:16 -0800, CptDondo <yan@NsOeSiPnAe Mr.comwrote:
    >Could someone please let me know the equivalent of C atoi and similar
    >functions? I need to convert a hex text string - e.g. '0x01' - to its
    >integer equivalent.
    hexdec after stripping off the leading '0x'?

    >I also want to be able to print the contents of a binary string as its
    >integer values:
    >
    >$x = pack("n",0x20);
    >print $x;
    >
    >prints one space, as 0x20 is the ascii representation for a ' '.
    I think you want unpack:

    <?php
    $var = chr(0x20) . chr(0x21) . chr(0x22);
    print join(" ", unpack('C*', $var));
    ?>

    --
    Andy Hassall :: andy@andyh.co.u k :: http://www.andyh.co.uk
    http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool

    Comment

    • Curtis

      #3
      Re: atoi and similar functions?

      PHP is a loosely and dynamically typed language, so there's no need to
      explicitly convert from string to integer, in most cases.

      If you want to convert from hex to decimal, check out the hexdec
      function. See: http://php.net/hexdec

      To convert a binary string to an integer, you can use bindec(): See


      $dec = intval(bindec($ binaryString));


      Curtis

      On Jan 8, 9:46 am, CptDondo <y...@NsOeSiPnA eMr.comwrote:
      Could someone please let me know the equivalent of C atoi and similar
      functions? I need to convert a hex text string - e.g. '0x01' - to its
      integer equivalent.
      >
      I also want to be able to print the contents of a binary string as its
      integer values:
      >
      $x = pack("n",0x20);
      print $x;
      >
      prints one space, as 0x20 is the ascii representation for a ' '.
      >
      My C background really blinds me sometimes....
      >
      --Yan

      Comment

      Working...