Using pack to write Arabic char(s) to a file.
<?php
$text = "㔆㘆㘆";
$text = mb_convert_encoding($text, "UCS-2BE", "HTML-ENTITIES");
$len = mb_strlen($text);
$bom = mb_convert_encoding("", "unicode", "HTML-ENTITIES");
$fp = fopen('text.txt', 'w');
fwrite($fp, pack('a2', $bom));
fwrite($fp, pack("a{$len}", $text));
fwrite($fp, pack('a2', $bom));
fwrite($fp, pack('a2', "\n"));
fclose($fp);
?>
pack
(PHP 4, PHP 5)
pack — Veriyi bir ikil dizge halinde paketler
Açıklama
Belirtilen değiştirgeleri belirtilen biçem biçeminde
bir ikil bir dizge olarak paketler.
Anafikir Perl'den alınmış olup, tüm biçemleme kodları Perl'deki gibi çalışır. Ancak, biçemleme türleri aynı değildir, örneğin "u" biçemi Perl'de yoktur.
Şuna dikkat edin: İşaretli ve işaretsiz değerler arasındaki ayrım sadece unpack() işlevini etkiler. pack() işlevi her ikisi için de aynı sonuçları üretir.
Ayrıca, şuna da dikkat edin: PHP, işaretli değerleri makineye bağımlı tamsayı değerler olarak saklar. Eğer float türünde tam hassasiyetle saklanamayacak kadar büyük bir işaretsiz tamsayı verirseniz istenmeyen sonuçlar alabilirsiniz.
Değiştirgeler
-
biçem -
biçemdzgesi isteğe bağlı bir yineleme değiştirgesi ile öncelenmiş biçem kodlarından oluşur. Yineleme değiştirgesi bir tamsayı değer veya girdi verisinin sonuna kadar yineleme belirten bir * olabilir. a, A, h, H için yineleme sayısı, bir veri değiştirgesinden kaç karakter alındığını belirtir. @ için yineleme sayısı ise, sonraki verinin konacağı mutlak konumu belirtir. Bunlar dışında kalan herşey için yineleme sayısı, sonuçlanan ikil dizgede paketlenmek üzere kaç tane veri argümanın toplanacağını belirtir.Şimdilik gerçeklenmiş biçemler:
pack() biçem karakterleri Kod Açıklama a NUL-dolgulu dizge A BOŞLUK dolgulu dizge h Onaltılık dizge, küçük dörtlü önce gelir H Onaltılık dizge, büyük dörtlü önce gelir c işaretli bayt C işaretsiz bayt s İşaretli kısa tamsayı (daima 16 bit olup, makine bayt sıralamasındadır) S İşaretsiz kısa tamsayı (daima 16 bit olup, makine bayt sıralamasındadır) n İşaretsiz kısa tamsayı (daima 16 bit olup, en kıymetli bayt baştadır) v İşaretsiz kısa tamsayı (daima 16 bit olup, en kıymetsiz bayt baştadır) i İşaretli tamsayı (boyut ve bayt sıralaması makineye bağlıdır) I İşaretsiz tamsayı (boyut ve bayt sıralaması makineye bağlıdır) l İşaretli uzun tamsayı (daima 32 bit olup, makine bayt sıralamasındadır) L İşaretsiz uzun tamsayı (daima 32 bit olup, makine bayt sıralamasındadır) N İşaretsiz uzun tamsayı (daima 32 bit olup, en kıymetli bayt baştadır) V İşaretsiz uzun tamsayı (daima 32 bit olup, en kıymetsiz bayt baştadır) f Tek hassasiyetli gerçek sayı - float (boyut ve gösterim makineye bağlıdır) d Çift hassasiyetli gerçek sayı - double (boyut ve gösterim makineye bağlıdır) x NUL bayt (0. karakter) X Bir baytlık yedek @ Bir mutlak konuma kadar NUL dolgu -
değiştirgeler -
Dönen Değerler
Veriyi içeren bir ikil dizge döner.
Örnekler
Örnek 1 - pack() örneği
<?php
$ikilveri = pack("nvc*", 0x1234, 0x5678, 65, 66);
?>
Elde edilen ikil dizge 6 bayt uzunlukta olur ve sırayla 0x12, 0x34, 0x78, 0x56, 0x41, 0x42 baytlarını içerir.
If you're bugged by http://bugs.php.net/bug.php?id=5889 then you can try this:
Use igbinary-serialize if you don't mind a little overhead.
Or intarray (by the same person) if you don't mind using a slightly experimental package which may have problems sharing data between differently byte/bit ordered architectures.
I don't believe it would be too difficult to shove a serialize function and unserialize function in there if you rip out the code from igbinary for storing numeric arrays with the correct endianess. Looking at `igbinary_serialize32` and `igbinary_unserialize32` in igbinary.c it should be very easy to copy that functionality to intarray.c.
Take away the "<<0" though, that's just stupid :P
Ref: http://opensource.dynamoid.com/
When trying to create a ZIP file using the pack function - I experienced trouble with the "a" code - It converted all chars correct from the std. ASCII charset but not more language specific like ÆøÅ.
It seems that ZIP files do not use the same HEX for these as everything else does.
The fix was a quick workaround but you'll probably get the picture:
function UniHex($str) {
// æ ø å Æ Ø Å
//These are simply one HEX code being replaced by another to correct the issue
$except = array("E6"=>"91","F8"=>"9B","E5"=>"86","C6"=>"92","D8"=>"9D", "C5"=>"8F");
for($i = 0; $i < strlen($str); $i++) {
$hex = bin2hex(substr($str, $i, 1));
if ($except[strtoupper($hex)])
$hex = $except[strtoupper($hex)];
$return .= $hex;
}
return $return;
}
And then i replaced an "a100" code with "H".strlen(uniHex($mystring))
This is like i said a quick workaround, but if you find the real reason for this i'd be happy to see it
You will get the same effect with
<?php
function _readInt($fp)
{
return unpack('V', fread($fp, 4));
}
?>
or unpack('N', ...) for big-endianness.
If you'd like to understand pack/unpack. There is a tutorial here in perl, that works equally well in understanding it for php:
http://perldoc.perl.org/perlpacktut.html
Array pack:
<?php
function pack_array($v,$a) {
return call_user_func_array(pack,array_merge(array($v),(array)$a));
}
?>
You can use pack to strip the byte order mark (BOM) from a file.
For example, strip the UTF-8 BOM:
<?php
// Strips the UTF-8 mark: (hex value: EF BB BF)
function trimUTF8BOM($data){
if(substr($data, 0, 3) == pack('CCC', 239, 187, 191)) {
return substr($data, 3);
}
return $data;
}
?>
This function could be easily adjusted to match any byte order mark. Have a look at wikipedia for a full list of hex codes for each specific encoding.
- Gerard
These two functions allow conversion between binary string and signed integer with possibility to give the bit length.
Usage:
<?php
echo si2bin(-10, 32);
11111111111111111111111111110110
echo si2bin(10, 32);
00000000000000000000000000001010
echo bin2si("11111111111111111111111111110110", 32);
-10
echo bin2si("00000000000000000000000000001010", 32);
10
// signed integer to binary
function si2bin($si, $bits=32)
{
if ($si >= -pow(2,$bits-1) and $si <= pow(2,$bits-1) )
{
if ($si >= 0) // positive or zero
{
$bin = base_convert($si,10,2);
// pad to $bits bit
$bin_length = strlen($bin);
if ($bin_length < $bits) $bin = str_repeat ( "0", $bits-$bin_length).$bin;
}
else // negative
{
$si = -$si-pow(2,$bits);
$bin = base_convert($si,10,2);
$bin_length = strlen($bin);
if ($bin_length > $bits) $bin = str_repeat ( "1", $bits-$bin_length).$bin;
}
return $bin;
}
}
// binary to signed integer
function bin2si($bin,$bits=32)
{
if (strlen($bin)==$bits)
{
if (substr($bin,0,1) == 0) // positive or zero
{
$si = base_convert($bin,2,10);
}
else // negative
{
$si = base_convert($bin,2,10);
$si = -(pow(2,$bits)-$si);
}
return $si;
}
}
?>
Even though in a 64-bit architecure intval(6123456789) = 6123456789, and sprintf('%b', 5000000000) = 100101010000001011111001000000000
pack will not treat anything passed to it as 64-bit. If you want to pack a 64-bit integer:
<?php
$big = 5000000000;
$left = 0xffffffff00000000;
$right = 0x00000000ffffffff;
$l = ($big & $left) >>32;
$r = $big & $right;
$good = pack('NN', $l, $r);
$urlsafe = str_replace(array('+','/'), array('-','_'), base64_encode($good));
//done!
//rebuild:
$unurl = str_replace(array('-','_'), array('+','/'), $urlsafe);
$binary = base64_decode($unurl);
$set = unpack('N2', $tmp);
print_r($set);
$original = $set[1] << 32 | $set[2];
echo $original, "\\r\\n";
?>
results in:
Array
(
[1] => 1
[2] => 705032704
)
5000000000
but ONLY on a 64-bit enabled machine and PHP distro.
Be aware of format code H always padding the 0 for byte-alignment to the right (for odd count of nibbles).
So pack("H", "7") results in 0x70 (ASCII character 'p') and not in 0x07 (BELL character)
as well as pack("H*", "347") results in 0x34 ('4') and 0x70 ('p') and not 0x03 and 0x47.
/* Convert float from HostOrder to Network Order */
function FToN( $val )
{
$a = unpack("I",pack( "f",$val ));
return pack("N",$a[1] );
}
/* Convert float from Network Order to HostOrder */
function NToF($val )
{
$a = unpack("N",$val);
$b = unpack("f",pack( "I",$a[1]));
return $b[1];
}
Note that the the upper command in perl looks like this:
$binarydata = pack ("n v c*", 0x1234, 0x5678, 65, 66);
In PHP it seems that no whitespaces are allowed in the first parameter. So if you want to convert your pack command from perl -> PHP, don't forget to remove the whitespaces!
Coder's example is basically an explanation of bindec() and decbin(), not pack() and unpack().
Here's some code to convert a string binary expression into its binary-string equivalent and vice versa.
(Would be even simpler if pack/unpack offered a 'b' format code....)
<?php
function bin2bstr($input)
// Convert a binary expression (e.g., "100111") into a binary-string
{
if (!is_string($input)) return null; // Sanity check
// Pack into a string
return pack('H*', base_convert($input, 2, 16));
}
function bstr2bin($input)
// Binary representation of a binary-string
{
if (!is_string($input)) return null; // Sanity check
// Unpack as a hexadecimal string
$value = unpack('H*', $input);
// Output binary representation
return base_convert($value[1], 16, 2);
}
// Returns string(3) "ABC"
var_dump(bin2bstr('01000001 01000010 01000011'));
// Returns string(24) "010000010100001001000011"
var_dump(bstr2bin('ABC'));
?>
Useful function to swap bytes in long using pack/unpack:
<?php
function bswap($long)
{
// Convert long to 4-byte string in native byte order,
// unpack string to array with 4 elements (indices are started from 1 sic!):
$x = unpack("C4", pack("L", $long));
// Swap array elements and pack into 4-byte string,
// convert string to long in native byte order:
// PHP don't allow to use brackets ([]) after function call. So we need to use temp variable:
$x2 = unpack("L", pack("C4", $x[4], $x[3], $x[2], $x[1]));
return $x2[1];
}
?>
Unfortunatelly function can't be minified without using a lot of arithmetic and bit masks.
<?php
// SAMPLE USAGE:
$ip1 = '65.66.67.168';
$ip2 = long2ip(bswap(ip2long($ip1)));
echo "ip1 = $ip1\n";
echo "ip2 = $ip2\n";
// output:
// ip1 = 65.66.67.168
// ip2 = 168.67.66.65
?>
If you need to unpack a signed short from big-endian or little-endian specifically, instead of machine-byte-order, you need only unpack it as the unsigned form, and then if the result is >= 2^15, subtract 2^16 from it.
And example would be:
<?php
$foo = unpack("n", $signedbigendianshort);
$foo = $foo[1];
if($foo >= pow(2, 15)) $foo -= pow(2, 16);
?>
<?PHP
function ntohs($port) {
$b=pack("N", $port);
return substr($b,2,2);
}
?>
I've spent a number of hours (n>=2) finding how to do this,
it works like the c function 'ntohs', used for eg the socks5 proxy protocol.
This is how I used pack to convert base2 to base64 since base_convert doesn't support base64
The base conversions don't work for long strings, which is why I convert 1 byte at a time
Hope this helps someone
function base2to64($base2) {
if ($remainbits = strlen($base2)%8) $base2 .= str_repeat('0',8-$remainbits);
$base64 = NULL;
for ($i=0;$i<strlen($base2);$i+=8) $base16 .= sprintf('%02x',bindec(sprintf('%08d',substr($base2,$i,8))));
return base64_encode(pack('H*',$base16));
}
function base64to2($base64) {
list($base16) = unpack('H*0',base64_decode($base64));
$base2 = NULL;
for ($i=0;$i<strlen($base16);$i++) $base2 .= sprintf('%04d',base_convert(substr($base16,$i,1),16,2));
return $base2;
}
This is how you can produce a code that is in fact a picture.
(This code is a complete tool, copy it to a file, call it 'somehow.php' and produce your pictures as hexcode).
<!--// ***Begin of File*** //-->
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>" enctype="multipart/form-data">
<input type="file" name="thefile"><input type="submit">
</form>
<?php
$rh = fopen ($_FILES['thefile']['tmp_name'], "r");
$pb = fread($rh, 8192);
fclose($rh);
$pc = bin2hex($pb);
$pd = wordwrap($pc, 76, "\".<br /> \n \"", 1);
echo "<TT>\$hexpic=\""."$pd"."\"\n</TT>;";
?>
<!--// ***End of File*** //-->
Copy the result in your site code somewhere. For to show the code as a picture you can use something like what dirk (at) camindo de wrote ...
<?php
$hexpic=".......................
.....................";
$data = pack("H" . strlen($hexpic), $hexpic);
header("Content-Type: image/png");
// maybe your is jpeg / gif / png
header("Last-Modified: " . date("r", filectime($_SERVER['SCRIPT_FILENAME'])));
header("Content-Length: " . strlen($data));
echo $data;
?>
have fun!
a cool function to converrt numbers to Persian numbers(utf-8)
origin: http://www.farsiweb.info/jalali/jalali.phps
function farsinum($str)
{
$ret = "";
for ($i = 0; $i < strlen($str); ++$i) {
$c = $str[$i];
if( $c >= '0' && $c <= '9' )
$out .= pack("C*", 0xDB, 0xB0 + $c);
else
$ret .= $c;
}
return $ret;
}
Unlike the PERL pack function, the PHP version does not accept arrays as arguments (see PHP Bugs: #5889).
To get around this I found something like this works:
<?php
$word_data = array(132,457,234,63);
$packet_send = "";
foreach ($word_data as $word) {
$packet_send = $packet_send.pack("v",$word);
}
?>
As Pack returns a string, so you can just concatenate them.
Work around newsletter tracking:
include a transparent gif (1x1 pixel) with url = track.php and parameters.
track.php has to write the parameters e.g. into a database and provides the gif - using following code:
header("Content-Type: image/gif");
header("Content-Length: 49");
echo pack('H*',
'47494638396101000100910000000000ffffffff'
.'ffff00000021f90405140002002c000000000100'
.'01000002025401003b'
);
I needed to convert binary values from a file to integers.
Maybe there is something simpler, but the snippets i saw above seemed a little convoluted:
function bin2asc ($binary)
{
$val = 0;
for ($i = strlen($binary) - 1; $i >= 0; $i--) {
$ch = substr($binary, $i, 1);
$val = ($val << 8) | ord($ch);
}
return $val;
}
This was called like the following from a binary file:
function _readInt($fp)
{
return bin2asc(fread($fp, 4));
}
Note that the for loop should be reversed for network byte order instead of intel byte order. Also the conversion will work with any number of bytes, but will happily overflow.
take note: if you produce binary files using PHP on multiple platforms, that you use one of the machine-independent pack options.
This means 's' 'S' 'i' 'I' 'd' and 'f' are _EVIL_ :) Took me some time to figure out what my Excel-generator what futzing about :) Turned out the production machine was a Sun Sparc. I develop on my own x86 Linux server.
Hope this helps anyone...
c-ya,
Jurgen
If you are trying to do ascii <--> binary conversions like me;
you probably found that unlike the perl pack functions, these wont help too much. Attached are two functions I wrote to accomplish this task.
<br>
function bin2asc ($binary)
{
$i = 0;
while ( strlen($binary) > 3 )
{
$byte[$i] = substr($binary, 0, 8);
$byte[$i] = base_convert($byte[$i], 2, 10);
$byte[$i] = chr($byte[$i]);
$binary = substr($binary, 8);
$ascii = "$ascii$byte[$i]";
}
return $ascii;
}
<br>
function asc2bin ($ascii)
{
while ( strlen($ascii) > 0 )
{
$byte = ""; $i = 0;
$byte = substr($ascii, 0, 1);
while ( $byte != chr($i) ) { $i++; }
$byte = base_convert($i, 10, 2);
$byte = str_repeat("0", (8 - strlen($byte)) ) . $byte; # This is an endian (architexture) specific line, you may need to alter it.
$ascii = substr($ascii, 1);
$binary = "$binary$byte";
}
return $binary;
}
<br>
Im not sure these are the most efficient functions, but surely alot faster than loading up a perl interpreter for every binary conversion =)
may be useful
<?php
public function unpack_str($str, $len) {
$tmp_arr = unpack("c".$len."chars", $str);
$out_str = "";
foreach($tmp_arr as $v) {
if($v>0) {
$out_str .= chr($v);
}
}
return $out_str;
}
public function pack_str($str, $len) {
$out_str = "";
for($i=0; $i<$len; $i++) {
$out_str .= pack("c", ord(substr($str, $i, 1)));
}
return $out_str;
}
?>
