Hello, please sign in or register
You are here: Home

PHP Pack function

Understanding pack and the bitwise php operators. This article has spurned off my previous article on comunicating binary data for DNS.

As a webdeveloper i can use use extensions to send and recieve most binary data. Albeit i've only recently hit a wall where i have to use binary data, sockets and painful revising of RFC documents. The following table represents output from the pack function e.g.

echo pack( 'c*', 0x41, 0x61, 0x6162, 255, "255");

The first parameter is how we are interpretating the subsequent arguments. More information is available at http://php.net/pack on this.

  0x41 0x61 0x6162 255 "255"  
c* A a b signed char
C* A a b unsigned char
a* 65 97 24930 255 255 NUL-padded string
A* 65 97 24930 255 255 SPACE-padded string
h* V y B9 R R Hex string, low nibble first
H* e $“ %P %P Hex string, high nibble first
v* A a ba unsigned short (always 16 bit, little endian byte order)
n* A a ab signed integer (machine dependent size and byte order)
S* A a ba unsigned short (always 16 bit, machine byte order)


Recreate the table

echo '<table><tr>';
foreach( array('0x41', '0x61', '0x6162', '255', '"255"') as $o )
		echo '<th>'.$o.'</th>';
echo '</tr>';

foreach( array('c*','C*','a*','A*','h*','H*','v*','n*','S*') as $o ){
	echo '<tr><th>' . $o . '</th>';
	foreach( array(0x41, 0x61, 0x6162, 255, "255") as $oo )
		echo '<td>' . htmlentities(pack ( $o, $oo )) . '</td>';
	echo '</tr>' . "\n";
echo '</table>'; 



Is there any way to convert a Hex code to 16bit (binary ) string The output looks like this :� �
Created 03/12/09
But the replica watches sentry reebok shoes knew abercrombie and fitch uk his
Created 07/10/15
“I canada goose outlet think it doesn’t swarovski jewelry bode very
Created 21/01/16
The michael--kors.org.uk next ralph lauren outlet day patriots jersey Mrs
Created 01/03/16
Tyrion 5c cases watched her ray ban sunglasses read. His timber...
Created 18/04/16
Bond grinned. pandora-bracciali.it "We bcbgmax.in.net only chaussure...
Created 15/06/16
This hilfiger outlet time swarovski jewelry something really timberland outlet strange, ...
Created 30/06/16
ninest123 One canada goose pas cher gucci outlet thing
Created 21/07/16

Prove you are not a robot

To prove you are not a robot, please type in the six character code you see in the picture below
Security confirmation codeI can't see this!
Email never shown*
Home Page


Andrew Dodson
Since:Feb 2007

Comment | flag


Bookmark and Share