in reply to Converting Binary to hex

What's going wrong is that you are assigning a numeric value to $binaddr, but treating it as though it contained the string '100010', while it actually has the value 34, so oct is being asked to convert the string '0b34'.

If you quote the 100010, then it will work as expected:

#!perl $binaddr = '100010'; $hexaddr = sprintf("%x", oct("0b$binaddr")); print $hexaddr;

Replies are listed 'Best First'.
Re^2: Converting Binary to hex
by AnomalousMonk (Archbishop) on Oct 01, 2016 at 00:37 UTC
    ... assigning a numeric value to $binaddr, but treating it as though it contained the string '100010' ...

    But isn't the stringization of 100,010 (decimal) exactly '100010'?

    c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "my $n = 100_010; my $b = qq{0b$n}; dd $n, $b; " (100010, "0b100010")
    In this case, quoting is not needed.


    Give a man a fish:  <%-{-{-{-<

      :) no
      printf '%b', 100_010; __END__ 11000011010101010