Welcome to the monastery.
Using the data you've provided, here's a blow-by-blow account of how to achieve what I believe you're after.
# Simulate the 4 bytes of input binary data (i.e. from input file)
my $in_data = pack 'B32' => '01011111111100000111000011111010';
# Create new leading byte with binary value of 1
my $lead_byte = pack 'B8' => '00000001';
# Concatenate leading and input bitstrings
my $concat_bitstrings = unpack('B8', $lead_byte) . unpack('B32', $in_d
# Create new data with 5 (1+4) bytes
my $out_data = pack 'B40' => $concat_bitstrings;
# Test it worked
# Converting "0101_1111_1111_0000_0111_0000_1111_1010"
# and "0000_0001_0101_1111_1111_0000_0111_0000_1111_1010"
# to HEX format for ease of visualising/checking
my $in_hex = '5ff070fa';
my $out_hex = '01' . $in_hex;
# Print expected and actual results
say 'EXPECT: ', $in_hex;
say 'GOT: ', unpack 'H*' => $in_data;
say 'EXPECT: ', $out_hex;
say 'GOT: ', unpack 'H*' => $out_data;
I'll assume you've found the doco for pack and unpack. There's also a tutorial you may not be aware of: perlpacktut.
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:
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
| & || & |
| < || < |
| > || > |
| [ || [ |
| ] || ] ||