I've never dealt with bits and bytes, so I'm unable to figure out the first 5 bits and last 3 bits of say 129... supposed to get 16 and 3 or 2 or something... Thank you!
I sort of came up with this, but it's not nice:
#!/usr/bin/perl
#
$num = 129;
$str = unpack("B32", pack("N", $num));
$str =~ s/^0+(?=\d)//; # otherwise you'll get leading zeros
print "$str\n";
$fragment = substr $str, 0, 5; #first 5 bytes
print "$fragment\n";
$num1=unpack("N", pack("B32", substr("0" x 32 . $fragment, -32)));
print "num1=$num1\n";
$fragment = substr $str, 5, 3; #first 5 bytes
print "$fragment\n";
$num2=unpack("N", pack("B32", substr("0" x 32 . $fragment, -32)));
print "num2=$num2\n";
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.