473,320 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 1597
$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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Randell D. | last post by:
Folks, I have a box that expects numeric input - I'd like to test for it (yeah, I'll have service side checks too but also want client checks). How can I test that my input is numeric - and...
2
by: anonieko | last post by:
This applies to javascript dynamic textbox onkey > > > Newsgroups: comp.lang.javascript From: Lasse Reichstein Nielsen <l...@hotpop.com> - Find messages by this author Date: Fri, 15 Jul 2005...
7
by: John J. Hughes II | last post by:
I need to save a DWORD to the sql server, the below posts an error, any suggestions on what I am doing wrong. I have the column in the sql server defined as an int since unsigned int is not valid....
11
by: Keith | last post by:
I apologize for those of you who think I'm posting on the same topic. It is not that I don't appreciate all of your comments - and I'm definitely reading them all - but I think I have a differing...
20
by: MLH | last post by:
120 MyString = "How many copies of each letter do you need?" 150 MyVariant = InputBox(MyString, "How Many?", "3") If MyVariant = "2" Then MsgBox "MyVariant equals the string '2'" If...
1
by: liuliuliu | last post by:
hi -- sorry if this is trivial -- but how do you make a screenshot of a pygame display? i have a surface which is basically the entire visible screen -- how do you write this surface as an image...
13
by: nishit.gupta | last post by:
Is their any fuction available in C++ that can determine that a string contains a numeric value. The value cabn be in hex, int, float. i.e. "1256" , "123.566" , "0xffff" Thnx
5
by: Mike | last post by:
I use MS SQL EXPRESS DB VS 2005, c# Win Application I have problem "The string is non-numeric" with formula CDbl({pr_DajPonudu;1.KolicinskiPopust})/100 * {@NajamProstora}
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.