Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

G'day ashes3d,

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.

#!/usr/bin/env perl use 5.010; use strict; use warnings; # 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 +ata); # 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 'Input'; say 'EXPECT: ', $in_hex; say 'GOT: ', unpack 'H*' => $in_data; say 'Output'; say 'EXPECT: ', $out_hex; say 'GOT: ', unpack 'H*' => $out_data;

Output:

$ pm_bin_32_40.pl Input EXPECT: 5ff070fa GOT: 5ff070fa Output EXPECT: 015ff070fa GOT: 015ff070fa

I'll assume you've found the doco for pack and unpack. There's also a tutorial you may not be aware of: perlpacktut.

-- Ken


In reply to Re^3: PERL Binary Data Handling by kcott
in thread PERL Binary Data Handling by ashes3d

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2024-04-19 23:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found