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

Wide Character in oct when using the below code

$binaddr = 100010; $hexaddr = sprintf("%x", oct("0b$binaddr"));

Replies are listed 'Best First'.
Re: Converting Binary to hex
by BrowserUk (Patriarch) on Sep 30, 2016 at 06:25 UTC
    $s = '10101010'; printf "%x\n", eval "0b$s";; aa printf "%x\n", ord pack 'B*', $s;; aa printf "%x\n", unpack 'V', pack 'B32', $s;; aa printf "%x\n", unpack 'S', pack 'B16', $s;; aa printf "%x\n", unpack 'C', pack 'B8', $s;; aa

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    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". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Converting Binary to hex
by Laurent_R (Canon) on Sep 30, 2016 at 06:09 UTC
    Hi Himaja,

    your $binadddr variable is assigned to a decimal integer, not to a binary integer.

    If you expect $binaddr to be a binary integer, then you should use:

    $binadddr = 0B100010

      actually $binaddr is read from a hash. so the value is something like initialvalue = '0100010' which i read into $binaddr. So i am appending 0b while passing it to hex function. Looks like the format of the data extracted from the hash cannot be directly sent to hex function. Any idea?

        Hmm, I can't test right now, but the 0B prefix will work, I think, only for an initial numerical literal assignment. Once the value is in memory, you can no longer do it his way and you probably need to make an explicit conversion from binary string to whatever you need.
Re: Converting Binary to hex
by AnomalousMonk (Archbishop) on Sep 30, 2016 at 15:48 UTC

    I don't even see the originally reported problem:

    c:\@Work\Perl\monks>perl -wMstrict -le "my $binaddr = 100010; print qq{'$binaddr'}; ;; my $hexaddr = sprintf '%x', oct qq{0b$binaddr}; print qq{'$hexaddr'}; " '100010' '22'
    What output were you expecting from the original input?

    Update: And for good measure:
    c:\@Work\Perl\monks>perl -wMstrict -le "my %h = (B => 100010); ;; my $hexaddr = sprintf '%x', oct qq{0b$h{B}}; print qq{'$hexaddr'}; " '22'

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

Re: Converting Binary to hex
by RonW (Parson) on Sep 30, 2016 at 22:14 UTC

    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;
      ... 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