This is a discussion on How to save numeric value as a 32bit number rather than string? within the PHP Language forums, part of the PHP Programming Forums category; <? $fp = fopen ("binary.dat", "wb"); $buffer = 12345; fwrite ($fp, $buffer); fclose($fp); ?> That creates ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
<?
$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? |
|
|||
|
$someNum = 12345;
Convert to binary: $binNum = base_convert($someNum, 10, 2); Convert to hex: $hexNum = base_convert($someNum, 10, 16); http://www.php.net/manual/en/function.base-convert.php |
|
|||
|
On Wed, 27 Apr 2005 02:00:50 +0800, "Dave Turner" <nobody@nowhere.nohow> wrote:
><? > $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? http://uk.php.net/manual/en/function.pack.php -- Andy Hassall / <andy@andyh.co.uk> / <http://www.andyh.co.uk> <http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool |
|
|||
|
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@gmail.com> wrote in message news:1114539519.183425.6840@g14g2000cwa.googlegrou ps.com... > $someNum = 12345; > > Convert to binary: > $binNum = base_convert($someNum, 10, 2); > > Convert to hex: > $hexNum = base_convert($someNum, 10, 16); > > http://www.php.net/manual/en/function.base-convert.php > |