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

I have a problem :) I have the following in hex 82 (1000 0010). This represents two values: The 2 MSB represents one value. In this case 2 and the remaining 6 bits another value in this case 2. Does anyone know how to extract this in perl? Regards, Mathias

Replies are listed 'Best First'.
Re: converting hex to decimal
by jmcnamara (Monsignor) on Nov 04, 2003 at 11:45 UTC

    Have a look at the bitwise operators in perlop
    #!/usr/bin/perl -wl my $n = 0x82; printf "%08b\n", $n; printf "%08b\n", $n >> 6; # 2 msb printf "%08b\n", $n & 0x3f; # 6 lsb __END__ Prints: 10000010 00000010 00000010

    --
    John.

Re: converting hex to decimal
by tachyon (Chancellor) on Nov 04, 2003 at 11:38 UTC
    # convert to decimal using hex function my $bitmask = hex(82); printf "Dec $bitmask (%b)\n", $bitmask, $bitmask; # mask off bits we want with binary and my $val1 = $bitmask & 192; # 192 is 11000000 bin # bitshift 6 to right to discard insignificant bits $val1 >>= 6; print "Val1 = $val1\n"; print "Val2 = ", $bitmask & 63; # 63 is 111111 bin __DATA__ Dec 130 (10000010) Val1 = 2 Val2 = 2

    cheers

    tachyon

      Thanks!! I Appreciate your help! Cheers, Mathias
Re: converting hex to decimal
by Anonymous Monk on Nov 04, 2003 at 10:52 UTC
    So what do you want to do, extract, convert, or what?