PHP | pack() Function
The pack() function is an inbuilt function in PHP which is used to pack the given parameter into a binary string in a given format.
Syntax:
pack( $format, $arguments )
Parameters: This function accepts two parameters as mentioned above and described below:
- $format: It is required parameter. It specifies the format to be used while packing the data. The possible values of format are:
- a β string which is NUL-padded
- A β string which is SPACE-padded
- h β low nibble first Hex string
- H β high nibble first Hex string
- c β signed character
- C β unsigned character
- s β signed short (16 bit, machine byte order)
- S β unsigned short ( 16 bit, machine byte order)
- n β unsigned short ( 16 bit, big endian byte order)
- v β unsigned short ( 16 bit, little endian byte order)
- i β signed integer (machine dependent byte order and size)
- I β unsigned integer (machine dependent byte order and size)
- l β signed long ( 32 bit, machine byte order)
- L β unsigned long ( 32 bit, machine byte order)
- N β unsigned long ( 32 bit, big endian byte order)
- V β unsigned long ( 32 bit, little endian byte order)
- f β float (machine dependent representation and size)
- d β double (machine dependent representation and size)
- x β NUL byte
- X β Back up one byte
- Z β string which is NUL-padded
- @ β NUL-fill to absolute position
- $arguments: It is Optional parameter. It specifies one or more arguments to be packed.
Return Value: It returns a binary string containing data.
Note: This function is available on PHP 4.0.0 and newer version.
Program 1: This program uses C format to format the input parameter.
<?php echo pack( "C13" , 71, 69, 69, 75, 83, 70, 79, 82, 71, 69, 69, 75, 83); ?> |
Output:
w3wiki
Program 2: This program uses A format to format the input parameter.
<?php echo pack( "A3" , 71898); ?> |
Output:
718
Program 3: This program uses i format to format the input parameter.
<?php echo pack( "i3" , 56, 49, 54); ?> |
Output:
816
Reference: https://www.php.net/manual/en/function.pack.php
Contact Us