How to save numeric value as a 32bit number rather than string?

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

    How to save numeric value as a 32bit number rather than string?

    <?
    $fp = fopen ("binary.dat ", "wb");
    $buffer = 12345;
    fwrite ($fp, $buffer);
    fclose($fp);
    ?>

    That creates a file 5 bytes long that contains the text data "12345", but I
    need it to save that value as 32-bits instead (so the file would just be 4
    bytes, with the contents being 0x39300000).

    Anyone know how to do that?


  • ZeldorBlat

    #2
    Re: How to save numeric value as a 32bit number rather than string?

    $someNum = 12345;

    Convert to binary:
    $binNum = base_convert($s omeNum, 10, 2);

    Convert to hex:
    $hexNum = base_convert($s omeNum, 10, 16);



    Comment

    • Andy Hassall

      #3
      Re: How to save numeric value as a 32bit number rather than string?

      On Wed, 27 Apr 2005 02:00:50 +0800, "Dave Turner" <nobody@nowhere .nohow> wrote:
      [color=blue]
      ><?
      > $fp = fopen ("binary.dat ", "wb");
      > $buffer = 12345;
      > fwrite ($fp, $buffer);
      > fclose($fp);
      >?>
      >
      >That creates a file 5 bytes long that contains the text data "12345", but I
      >need it to save that value as 32-bits instead (so the file would just be 4
      >bytes, with the contents being 0x39300000).
      >
      >Anyone know how to do that?[/color]



      --
      Andy Hassall / <andy@andyh.co. uk> / <http://www.andyh.co.uk >
      <http://www.andyhsoftwa re.co.uk/space> Space: disk usage analysis tool

      Comment

      • Dave Turner

        #4
        Re: How to save numeric value as a 32bit number rather than string?

        Thanks, but not quite what I meant. That converts the base, but it's still a
        STRING ... ie. "11000000111001 "
        I need to save that as just 32-bits, so the file should only be 4 bytes


        "ZeldorBlat " <zeldorblat@gma il.com> wrote in message
        news:1114539519 .183425.6840@g1 4g2000cwa.googl egroups.com...[color=blue]
        > $someNum = 12345;
        >
        > Convert to binary:
        > $binNum = base_convert($s omeNum, 10, 2);
        >
        > Convert to hex:
        > $hexNum = base_convert($s omeNum, 10, 16);
        >
        > http://www.php.net/manual/en/function.base-convert.php
        >[/color]


        Comment

        • Dave Turner

          #5
          Re: How to save numeric value as a 32bit number rather than string?

          pack() is the one i'm after, thanks. :)


          Comment

          Working...