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

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 ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-26-2005
Dave Turner
 
Posts: n/a
Default 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?


Reply With Quote
  #2 (permalink)  
Old 04-26-2005
ZeldorBlat
 
Posts: n/a
Default Re: How to save numeric value as a 32bit number rather than string?

$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

Reply With Quote
  #3 (permalink)  
Old 04-26-2005
Andy Hassall
 
Posts: n/a
Default 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:

><?
> $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
Reply With Quote
  #4 (permalink)  
Old 04-27-2005
Dave Turner
 
Posts: n/a
Default 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@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
>



Reply With Quote
  #5 (permalink)  
Old 04-27-2005
Dave Turner
 
Posts: n/a
Default Re: How to save numeric value as a 32bit number rather than string?

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


Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 11:10 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0