By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,516 Members | 1,178 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,516 IT Pros & Developers. It's quick & easy.

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

P: n/a
<?
$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
Share this Question
Share on Google+
4 Replies


P: n/a
$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

P: n/a
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

P: n/a
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

P: n/a
pack() is the one i'm after, thanks. :)
Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.