Posted by Jon Slaughter on 06/16/07 04:14
"ZeldorBlat" <zeldorblat@gmail.com> wrote in message
news:1181962140.635446.239690@c77g2000hse.googlegroups.com...
> On Jun 15, 10:05 pm, "Jon Slaughter" <Jon_Slaugh...@Hotmail.com>
> wrote:
>> Is there any php functions to encode a decimal number(integer in this
>> case)
>> into a binary string? Not convert it to a string but encode it. So 255 =
>> chr(255) and 256 = chr(1).chr(0) and not 255 = '255'.
>>
>> Thanks,
>> Jon
>
> You mean like this:
>
> http://www.php.net/base_convert
>
No, I mean converting an int to its internal representation instead of its
string representation.
I wrote some methods to convert it but I'd like to use something faster if
possible:
function intDecStr($s)
{
$k = 1;
$l = 0;
for($i = 0; $i < 4; $i++)
{
$l = $l + $k*ord($s[$i]);
$k = $k*256;
}
return $l;
}
function intEncStr($i)
{
$s = "";
$q = 0;
do
{
$q++;
$m = $i % 256;
$s = $s.chr($m);
$i = floor($i/256);
} while ($q < 4);
return $s;
}
$i = 1343125;
$s = intEncStr($i);
$i = intDecStr($s);
[Back to original message]
|