Zeyukan Ich'
Zeyukan Ich'

Reputation: 693

PHP convert int 64 to int 8 bits

I would like to convert a number to the 8 bits equivalent, how is this possible?

I already tried to convert it to binary and read only 8 bits then convert in but this is not working since it is still considered as 64 bits encoded :

        var_dump(decbin($fileKey));
        var_dump(strlen($bin));
        var_dump(substr($bin, strlen($bin) - 8, 8));

What am I doing wrong? Is there an operator that will help me to do so?

Thanks

Upvotes: 0

Views: 295

Answers (1)

jspit
jspit

Reputation: 7703

Int 8 bit does not exist as a data type in PHP. As 8 bits or 1 byte you can only represent numbers from 0 to 255. The chr() function returns a one-character string containing the character specified by interpreting bytevalue as an unsigned integer.

Example:

$number = 16;
$bin = chr($number);

Upvotes: 1

Related Questions