Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

How do I convert a 32 bit unsigned integer to it's decimal value.

( #198085=categorized question: print w/ replies, xml ) Need Help??
Contributed by notfred on Sep 15, 2002 at 21:26 UTC
Q&A  > numbers


Description:

I have 4 bytes of data read in from a binary file. I need to treat this data as an 'int' in C, and get it's binary value. Perl wants to treat it as four characters, however. Is there any simple way to treat it as an integer, or convert it to an integer? Right now I have the painfully long:
sub BinWordToDec{ # There's GOT to be a better way to do this conversion.... # Converts a 32 bit binary word to a decimal integer. my $val = $_[0]; my $temp = "0b$val"; print "temp: $temp\n"; unless (length $val == 4) {return -1} my $bit1 = ord (substr $val,0,1); my $bit2 = ord (substr $val,1,1); my $bit3 = ord (substr $val,2,1); my $bit4 = ord (substr $val,3,1); $bit1 = unpack("B*", pack("N", $bit1)); $bit2 = unpack("B*", pack("N", $bit2)); $bit3 = unpack("B*", pack("N", $bit3)); $bit4 = unpack("B*", pack("N", $bit4)); my $dec = substr("0" x 8 . $bit1, -8).substr("0" x 8 . $bit2, -8). substr("0" x 8 . $bit3, -8).substr("0" x 8 . $bit4, -8); my $int = unpack("N", pack("B32", $dec)); $dec = sprintf("%d", $int); print "Decimal: \n", $dec, "\n"; return $dec; }
Thanks

Answer: How do I convert a 32 bit unsigned integer to it's decimal value.
contributed by BrowserUk

Given you are already using unpack, why not let it do the whole job?

perl> print unpack( 'N', chr(255).chr(255).chr(255).chr(255) ) 4294967295

The only caveat to this (or blakm's solution above) is that if the data is sourced from a system other than your own, the byte-order my be different (big-endian .v. little-endian ot vice versa) in which case you would need to use an unpack template of 'V'. The difference is demonstrated by

perl> print unpack( 'N', chr(255).chr(127).chr(64).chr(32) ) 4286529568 perl> print unpack( 'V', chr(255).chr(127).chr(64).chr(32) ) 541097983
If the source is a foriegn system, and you are unsure of the byte ordering, you'll need a sample known value and try both to make your determination of which of 'N' or 'V' to use.
Answer: How do I convert a 32 bit unsigned integer to it's decimal value.
contributed by blakem

my $dec = vec($binword,0,32);
Answer: How do I convert a 32 bit unsigned integer to it's decimal value.
contributed by Juerd

$dec = unpack('N', $val)
Answer: How do I convert a 32 bit unsigned integer to it's decimal value.
contributed by jmcnamara

$int = unpack 'N', $str;

You could also use unpack 'V' but based on your code 'N' is what you need.

Answer: How do I convert a 32 bit unsigned integer to it's decimal value.
contributed by notfred

figured it out:

return unpack("N",$val);
but, thanks :)

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others avoiding work at the Monastery: (4)
    As of 2014-09-21 12:28 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (168 votes), past polls