struct SupplyRequest {
time_t request_time; // hola de solicitud
int employee_id; // empleado que la realiza
char item[32]; // material solicitado
short quantity; // cantidad
short urgent; // ¿es urgente?
};
####
Desplz. Contenido (las direcciones de memoria aumentan hacia la derecha)
0 160 44 19 62| 41 82 3 0| 98 111 120 101 115 32 111 102
A0 2C 13 3E| 29 52 03 00| 62 6f 78 65 73 20 6f 66
| b o x e s o f
16 32 112 97 112 101 114 99 108 105 112 115 0 0 0 0 0
20 70 61 70 65 72 63 6c 69 70 73 00 00 00 00 00
p a p e r c l i p s
32 0 0 0 0 0 0 0 0| 2 0| 1 0
00 00 00 00 00 00 00 00| 02 00| 01 00
##
##
($order_time, $monk, $itemname, $quantity, $urgent) =
unpack( "l i Z32 s2", $rec );
##
##
pack ('a8',"hello") produce "hello\0\0\0"
pack ('Z8',"hello") produce "hello\0\0\0"
pack ('A8',"hello") produce "hello "
unpack('a8',"hello\0\0\0") produce "hello\0\0\0"
unpack('Z8',"hello\0\0\0") produce "hello"
unpack('A8',"hello ") produce "hello"
unpack('A8',"hello\0\0\0") produce "hello"
##
##
ord(pack('b8','00100110')) produce 100 (4 + 32 + 64)
ord(pack('B8','00100110')) produce 38 (32 + 4 + 2)
##
##
pack('h4','1234') produce 0x21,0x43
pack('H4','1234') produce 0x12,0x34
##
##
#!/usr/bin/perl -w
use strict;
# muestra el contenido de una cadena como bytes decimales y hexadecimales, y como caracteres
sub DumpString {
my @a = unpack('C*',$_[0]);
my $o = 0;
while (@a) {
my @b = splice @a,0,16;
my @d = map sprintf("%03d",$_), @b;
my @x = map sprintf("%02x",$_), @b;
my $c = substr($_[0],$o,16);
$c =~ s/[[:^print:]]/ /g;
printf "%6d %s\n",$o,join(' ',@d);
print " "x8,join(' ',@x),"\n";
print " "x9,join(' ',split(//,$c)),"\n";
$o += 16;
}
}
# efectuamos la solicitud
my $t = time;
my $emp_id = 217641;
my $item = "boxes of paperclips";
my $quan = 2;
my $urgent = 1;
my $rec = pack( "l i a32 s2", $t, $emp_id, $item, $quan, $urgent);
DumpString($rec);
# procesamos la solicitud
my ($order_time, $monk, $itemname, $quantity, $ignore) =
unpack( "l i a32 s2", $rec );
print "Order time: ",scalar localtime($order_time),"\n";
print "Placed by monk #$monk for $quantity $itemname\n";
# formatos de cadena
$rec = pack('a8',"hello"); # debería producir 'hello\0\0\0'
DumpString($rec);
$rec = pack('Z8',"hello"); # debería producir 'hello\0\0\0'
DumpString($rec);
$rec = pack('A8',"hello"); # debería producir 'hello '
DumpString($rec);
($rec) = unpack('a8',"hello\0\0\0"); # debería producir 'hello\0\0\0'
DumpString($rec);
($rec) = unpack('Z8',"hello\0\0\0"); # debería producir 'hello'
DumpString($rec);
($rec) = unpack('A8',"hello "); # debería producir 'hello'
DumpString($rec);
($rec) = unpack('A8',"hello\0\0\0"); # debería producir 'hello'
DumpString($rec);
# formatos de bits
$rec = pack('b8',"00100110"); # debería producir 0x64 (100)
DumpString($rec);
$rec = pack('B8',"00100110"); # debería producir 0x26 (38)
DumpString($rec);
# formatos en hexadecimal
$rec = pack('h4',"1234"); # debería producir 0x21,0x43
DumpString($rec);
$rec = pack('H4',"1234"); # debería producir 0x12,0x34
DumpString($rec);