in reply to golf anyone? (hexdump)


30 chars:
sub hexout4 { "@{[unpack('H*',pop)=~/../g]}" }
On a related note does anyone know why the sprintf format "%#x" behaves differently for 0 than for other numbers.
printf "%#x\n", 0; printf "%#x\n", 1; __END__ Prints: 0 0x1

I guess that the behaviour comes from the C compiler that builds perl because the following gives the same results (at least where I could test it):

int main () { printf("%#x\n", 0); printf("%#x\n", 1); return 0; }

So why is 0 treated differently?

--
John.

Replies are listed 'Best First'.
Re: Re: golf anyone? (hexdump)
by John M. Dlugosz (Monsignor) on Aug 09, 2002 at 15:05 UTC
    Perl implements its own printf formatting. But if it follows the ANSI/ISO C specification for what %#X does, it is per spec: any non-zero output is prefixed with 0x.

    To always have the prifix, just mention it in the string: "0x%X".

    I like your idea of using interpolation instead of join.

    —John