bcreane has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

I'm anticipating a "don't do that" response to this question, but I'm hoping for some clever trickery to help me solve this problem anyway.

I have a device driver that prints an int as an unsigned 32-bit value, so it displays "-1" as "4294967295."

My 64-bit perl v5.8.9 binary displays the same value as a twos-complement 64-bit value, so "-1" as "18446744073709551615."

I need to compare the values, ideally in string format.

I can't change the device driver, and I'm hoping to be able to use 64-bit perl. Does anyone know of a printf modifier (or some other approach) that specifies the length of the argument as 4 bytes? I know %hu works with perl, I'm hoping for something similar for 4-byte values.

Thanks!

Replies are listed 'Best First'.
Re: printf formatting 32 bit values
by BrowserUk (Patriarch) on Sep 28, 2010 at 21:39 UTC

    If you and the value with 0xffffffff, it will trim it to 32-bits:

    print unpack 'Q', pack 'Q', -1;; 18446744073709551615 print unpack 'Q', pack 'Q', -1 & 0xffffffff;; 4294967295

    Is that the sort of thing you are looking for?


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Yep, that does it. The pack'ing was the part I was missing ... sign extending 0x000000000ffffffff didn't work.

      thank you