Search notes:

Perl function: pack

pack function converts values to a byte sequence containing representations according to a given specification.
use warnings;
use strict;


my $hex    = '01' . '05' . '42' . 'f3';
my $string = pack('H*', $hex);
dump_string($string);


$string = "\x01\x05\x42\xf3";
dump_string($string);


my @shorts = (0, 1, 255, 256, 257, 258);
$string = pack('s6', @shorts);
dump_string($string);


sub dump_string {

   my $string = shift;

 # sort of emulate:
 # print unpack('H*', $string); print "\n";

   for my $byte (split //, $string) {
     printf "%.2x ", ord($byte);
   }

   print "\n";
}
Github repository about-perl, path: /functions/pack/basic.pl

See also

unpack

Index

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/developm...', 1758207258, '216.73.216.150', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/development/languages/Perl/functions/pack(71): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78