in reply to zero fill with pack

I'd like to use the "pack" function to produce what hoepfully will appear as "00 00 00 00 00 09 fe 00" when viewed in emacs with the "hexl-mode" turned on.

That's a 64-bit int, so
pack('q>', $_); # Signed pack('Q>', $_); # Unsigned

">" requires 5.10 (IIRC). "q" and "Q" require a 64-bit support. If you don't have a 64-bit support and your numbers aren't "very" large (<232), you could fake it using

pack('NN', $_<0 ? -1 : 0, $_); # Signed pack('NN', 0, $_); # Unsigned

In using "pack" I need to figure out how to fill with spaces ("\x20") before the actual hex representation of the 654848. Any suggestions?

You were talking about converting a number to a native int, now you're talking about its hex representation. It's not clear what you're asking here. Assuming this is a completely separate question,

sprintf('%8X', $_); # Padded with spaces sprintf('%08X', $_); # Padded with zeros

Update: Added answer to second question.

Replies are listed 'Best First'.
Re^2: zero fill with pack
by Spooky (Beadle) on Jun 22, 2010 at 18:03 UTC
    Ikegami, The 'NN' "fake-out" worked nicely. Two things though, I also need to pad the above value with two spaces, i.e., "00 00 00 00 00 09 fe 00 20 20" - need a total byte displacement of ten: how can I pad/add two more spaces at the end of my value? And, what is the significance of the ", 0," in the "pack('NN', 0, $_)" statement? Again, thanks for your prompt response!

      what is the significance of the ", 0," in the "pack('NN', 0, $_)" statement?

      The value for the first "N".

      Q=654848 ----------------------- 00 00 00 00 00 09 fe 00 ----------- ----------- N=0 N=654848

      I also need to pad the above value with two spaces, i.e., "00 00 00 00 00 09 fe 00 20 20"

      More concisely, you want to add two spaces. pack:

      C An unsigned char (octet) value. A A text (ASCII) string, will be space padded.
      pack('NN CC', 0, $_, ord(' '), ord(' ')) pack('NN CC', 0, $_, 0x20, 0x20) pack('NN A2', 0, $_, ' ') pack('NN n', 0, $_, 0x2020)
        AWESOME ikegami - Thanks!!
        ikegami - I found out I need to be able to handle one or two bytes worth of data adding nine or eight spaces: e.g., 00 20 20 20 20 20 20 20 20 20 or 00 00 20 20 20 20 20 20 20 20 Is there something smaller than an unsigned short (16bit)? I'm trying to figure out the correct pack template to use for the above examples.
Re^2: zero fill with pack
by BrowserUk (Patriarch) on Jun 22, 2010 at 21:54 UTC

    Your 32-bit solution is deficient.

    In that it fails for the 9 quadrillion integers between 2**32 ad 2**53 that 32-bit Perls can happily represent.