468,308 Members | 1,212 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,308 developers. It's quick & easy.

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?
Jul 17 '05 #1
4 1462
$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

Jul 17 '05 #2
On Wed, 27 Apr 2005 02:00:50 +0800, "Dave Turner" <no****@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 / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #3
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" <ze********@gmail.com> wrote in message
news:11********************@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

Jul 17 '05 #4
pack() is the one i'm after, thanks. :)
Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Randell D. | last post: by
7 posts views Thread by John J. Hughes II | last post: by
11 posts views Thread by Keith | last post: by
1 post views Thread by liuliuliu | last post: by
13 posts views Thread by nishit.gupta | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by Teichintx | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.