in reply to p0fq.pl and pack?

s, S and L use the system's endianness.
x86's s corresponds to universal v, with sign correction.
x86's S corresponds to universal v.
x86's L corresponds to universal V.

my $query = pack("L L L N N S S", ...);
should be
my $query = pack("V V V N N v v", ...);

... = unpack ("L L C Z20 Z40 c Z30 Z30 C C C s S N", $response);
should be
... = unpack ("V V C Z20 Z40 c Z30 Z30 C C C v v N", $response);
$score -= 65536 if $score > 32767;

$score -= 65536 if $score > 32767;
can also be written as
$score = unpack('s', pack('S', $score));

Replies are listed 'Best First'.
Re^2: p0fq.pl and pack?
by Anonymous Monk on Feb 19, 2007 at 23:27 UTC
    Thank you for the reply!

    I changed the pack and unpack to your suggestion, but I got "Bad response magic." error.

    perldoc -f pack:

    v An unsigned short in "VAX" (little-endian) order.
    V An unsigned long in "VAX" (little-endian) order. (These ’shorts’ and ’longs’ are _exactly_ 16 bits and _exactly_ 32 bits, respectively.)

    I don't understand what "$score -= 65536 if $score > 32767;" means, I don't have that in the original script, should I add it in?

      but I got "Bad response magic." error.

      What are the bytes of $response?

      I don't understand what "$score -= 65536 if $score > 32767;" means

      There is no code for "A signed short in 'VAX' (little-endian) order.", so I extracted an unsigned short, and that converts it to a signed short.

        I add
        print join(" ", map { sprintf "%#02x", $_ } unpack("C*",$response)), " +\n";
        it print out bytes like

        0xd 0xef 0xac 0xed 0x78 0x56 0x34 0x12 0x1 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

        Please pardon my ignorant :)
        How can I print out the bytes of $response?
        print $response gives me garbled strings