Dec 062013
 
Article PHP

Sometimes, a program need to process binary data. This post explains how to handle binary data in a PHP script for the most common processing needs.

Assign a binary value to a PHP variable using the built-in “chr” function

chr() returns a single byte, with the binary representation of the number passed as argument.

For instance, binary value “65” can be assigned to a variable $my_byte using the following sentence:

Being “65” (hex ox41) the ASCII code of the capital “A” character, the echo command above prints:

Function “ord”

There is also a built-in ord() function in PHP, that is the inverse of  chr(). ord() returns the ASCII code of a character passed as argument. The sentence:

prints:

Assign a binary value to a PHP variable using the built-in “pack” function

pack” converts into a binary string an array or string with its decimal, octal or hexadecimal representation.

For instance, a string of three bytes with values 23, 17, 208 can be assigned to a variable $data with the sentence:

The first argument of the pack function specifies the format used in the following argument(s) for the representation of the binary data.

In the example above, “C*” means that the arguments must be interpreted as the unsigned integer representation of the byte values.

The “unpack” function

PHP also implements an “unpack()” function, to convert binary data into its decimal, octal or hexadecimal representation, as requested in the format specifier passed as the first argument.

The following sample code prints the decimal representation of the byte values in the $data variable:

producing the following output:

If the “C*” format specifier is replaced with “H*”, the hexadecimal representation of the binary data will be printed:

In the examples above we can see that the the unpack() function:

  • returns an array of three elements whose values are integers when the “C*” format specifier is used
  • returns an array of a single element whose value is a string when the “H*” format specifier is used

Writing binary data to a file

To write binary data to a file, it has to be opened with the “b” flag, to avoid any conversion.

For instance, the following sample code writes 256 bytes to a file, with all the values 0,1,2,…,255 in sequence:

The result can be checked opening the file with a hexadecimal editor, or just dumping the content of the file to screen (the unix commands xxd or od can be used for this):

Reading a file holding binary data

To read a binary file, it has to be opened with the “b” flag, to avoid any kind of conversion.

In the next example, the file “binary-data.dat” generated in the previous example is read, and its contents are dumped to screen using the unpack() function to convert them to their decimal representation:

Extracting a substring from a binary string.

The same function substr used to extract a substring from a text string can also be used to extract a substring from a binary substring.

Besides, the value of a byte inside a binary string can be read and modified with the syntax used to access an element of an array.

Finally, the chr() function can be used to get the binary value of a byte

The following sample code makes use of these possibilities. In it:

  • a file is read 100 bytes at a time.
  • the number of bytes actually read is retrieved with a call to the strlen() function.
  • the value of the 5th byte (at index 4) is assigned the value chr(88)
  • finally, the sbustr function is used to extract and print each of the bytes in the binary string.

The output from the sample code above is:

Related posts

How to work with binary data in Perl

Index of posts related to PHP programming

 Posted by at 9:21 am

 Leave a Reply

(required)

(required)