You want unpack "B*" (not unpack "b*"), and it requires packed bytes.

$ perl -Mv5.14 -e' my $scabyte = 0x41; say unpack "B*", pack "C*", $scabyte; ' 01000001

When you already have unpacked bytes, you can use sprintf "%b".

$ perl -Mv5.14 -e' my $scabyte = 0x41; say sprintf "%08b", $scabyte; ' 01000001

#!/usr/bin/perl use v5.14; use warnings; my $packed_bytes = "ABCD"; my @bytes = unpack 'C*', $packed_bytes; my $bin = ''; for my $byte ( @bytes ) { printf "Decimal = [%1\$d] Hex = [%1\$02X] Binary = [%1\$08b]\n", $byte; $bin .= sprintf( "%08b", $byte ); } say ""; say $bin;
Decimal = [65] Hex = [41] Binary = [01000001] Decimal = [66] Hex = [42] Binary = [01000010] Decimal = [67] Hex = [43] Binary = [01000011] Decimal = [68] Hex = [44] Binary = [01000100] 01000001010000100100001101000100

#!/usr/bin/perl use v5.14; use warnings; my $packed_bytes = "ABCD"; my $bin = unpack "B*", $packed_bytes; say $bin;
01000001010000100100001101000100

In reply to Re: unpack Binary FAIL by ikegami
in thread unpack Binary FAIL by marinersk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.