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

<?php
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>'; 
?>

 

Comments

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

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!
Contact
Name*
Email never shown*
Home Page

Author

Andrew Dodson
Since:Feb 2007

Comment | flag

Categories

Bookmark and Share