int2bin()


string int2bin ( int $val, int $len [ , bool $swap=false ] )

Description

int2bin() converts an integer to a string (little endian data format) with optional swap function

※ available F/W version : all

Parameters

Return values

Returns the converted string, PHP error on error

Examples

<?php
$buf = "";
$buf .= int2bin(0x55, 4);          // 0x55 0x00 0x00 0x00
$buf .= int2bin(0x55, 8);          // 0x55 0x00 0x00 0x00 0x00 0x00 0x00 0x00

$buf .= int2bin(0x1122334455667788, 8);  // 0x88 0x77 0x66 0x55 0x44 0x33 0x22 0x11
$buf .= int2bin(0xaa88, 2, true);  // 0xaa 0x88 (with swap)
$buf .= int2bin(7000, 2);          // 0x58 0x1b

hexdump($buf);  
// OUTPUT
// 0000  55 00 00 00 55 00 00 00  00 00 00 00 88 77 66 55  |U...U........wfU|
// 0010  44 33 22 11 aa 88 58 1b                           |D3"...X.        |
?>

Use the following code if the length of input data exceeds the maximum length:

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

bin2int()

Remarks

None