bin2int()


int bin2int ( string $data, int $offset, int $len [ , bool $swap = false] )

Description

bin2int() converts a string to an integer, the size of $len from the offset (little endian data format) with optional swap function

※ available F/W version : all

Parameters

Return values

Returns the converted integer, PHP error on error

Example

<?php
$buf = "";
$buf = int2bin(0x11223344, 4);
$buf .= int2bin(0x55667788, 4);
hexdump($buf);
// OUTPUT: 0000 44 33 22 11 88 77 66 55 |D3"..wfU |
$i = bin2int($buf, 0, 1); // 1 byte from position 0
echo "i: $i\r\n"; // OUTPUT: i: 68
$i = bin2int($buf, 1, 2); // 2 bytes from position 1
echo "i: $i\r\n"; // OUTPUT: i: 8755
$i = bin2int($buf, 1, 2, true); // 2 bytes from position 1 with swap
echo "i: $i\r\n"; // OUTPUT: i: 13090
$i = bin2int($buf, 0, 4); // 4 bytes from position 0
echo "i: $i\r\n"; // OUTPUT: i: 287454020
$i = bin2int($buf, 0, 4, true); // 4 bytes from position 0 with swap
echo "i: $i\r\n"; // OUTPUT: i: 1144201745
$i = bin2int($buf, 0, 8); // 8 bytes from position 0
echo "i: $i\r\n"; // OUTPUT: i: 6153737367135073092
$i = bin2int($buf, 0, 8, true); // 8 bytes from position 0 with swap
echo "i: $i\r\n"; // OUTPUT: i: 4914309077090657877
?>
<?php
// It receives data from the UART0 and transmits data to the UART0 after changing 0x20 data to 0x2e

$pid = pid_open("/mmap/uart0"); // opens UART0
// set the device to 115200bps, no parity, 8 databit, 1stop bit
pid_ioctl($pid, "set baud 115200");
pid_ioctl($pid, "set parity 0");
pid_ioctl($pid, "set data 8");
pid_ioctl($pid, "set stop 1");
pid_ioctl($pid, "set flowctrl 0");

$rbuf = "";  // declaring $rbuf
$wbuf = "";  // declaring $wbuf
while(1)
{
    $rlen = pid_read($pid, $rbuf, 16); // read upto 16bytes data from the UART0

    if($rlen > 0)  // if there are received data
    {
        $wbuf = "";  // clear $wbuf
        for($i = 0; $i < $rlen; $i++)
        {
            // getting 1 byte data from the $rbuf at position $i
            $data = bin2int($rbuf, $i, 1);
            // changing $data to 0x2e if it is 0x20         
            if($data == 0x20) $data = 0x2e;  
            // append $data to the $wbuf
            $wbuf .= int2bin($data, 1);  
        }
    }
    pid_write($pid, $wbuf, $rlen);   // write the $rlen length of the $wbuf to the UART0
}
?>

See also

int2bin()

Remarks

None