Re: How can I write/read encoded numbers into/from a file?
| From: | Julio Sergio Santana | Date: | Thu, 08 Jul 2004 17:47:23 +0000 |
| Subject: | Re: How can I write/read encoded numbers into/from a file? | ||
| References: | 1 2 | Groups: | php.general php.general |
| Request: | Send a blank email to php-general+get-189984@lists.php.net to get a copy of this message | ||
Thank you Chris. I finally got the solution to my problem using the pack/unpack functions as you adviced me. The solution is:
<?
$i = 65;
$s = pack("l", $i);
echo strlen($s) . "\n";
echo "***$s***\n";
?>
X-Powered-By: PHP/4.1.2
Content-type: text/html
4
***A*** // This is "\0\0\0A"
Chris wrote:
Are you trying read the string '\0\0\0\0x41' from a file? or are you trying to read these 4 bytes from a file: 00000041 ? If the latter: The pack and unpack functions are designed to do this (and more). You could also manually do it yourself with something like this: $sBuff = "\0\0\0\0x41"; // Read the data into a var $iAnswer = ord($sBuff{0}) | ord($sBuff{1}) << 8 | ord($sBuff{2}) << 16 | ord($sBuff{3}) << 24; // Get the numerical value of each byte, then shift the bytes to get the 4 byte number. (Little Endian) Chris Julio Sergio Santana wrote:I'm just starting with PHP, and I've been browsing the whole manual to find how to solve the following problem: 1. An integer number is internally represented in 4 bytes. Say, for instance, number 65 is represented by 0x00000041, that is the string "\0\0\0\0x41", and not the string "65". 2. How can I write this representation into a file and then read back it from the file? Do you have any idea about this? Thank you, Sergio.