Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Pulling bytes out of string

by mike65535 (Novice)
on Oct 21, 2015 at 19:06 UTC ( [id://1145582]=perlquestion: print w/replies, xml ) Need Help??

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

I have a series of strings made up of two-byte data - (typical of CommPort communication: Command Word, length, text, "hex" values) Like this: 8602353907455755 ... I want to pull out the '86' and the '02' and the '35', etc., and put them into an array of bytes or something similar.

I've looked at "split". It seems to revolve around the use of Regular Expressions (of which I know less than I know about Perl) to determine delimiters (which I don't have).

Is there a function or operator within Perl that I can wrangle to perform the task?

Thanks.

Replies are listed 'Best First'.
Re: Pulling bytes out of string
by Kenosis (Priest) on Oct 21, 2015 at 19:35 UTC

    One option is to use a regex to match just two chars at a time--globally--and place the results into an array:

    use strict; use warnings; my $string = '8602353907455755'; my @arr = $string =~ /../g; print "@arr";

    Output:

    86 02 35 39 07 45 57 55

    Hope this helps!

Re: Pulling bytes out of string
by AnomalousMonk (Archbishop) on Oct 22, 2015 at 00:27 UTC

    An example using unpack:

    c:\@Work\Perl\monks>perl -wMstrict -le "my $string = '8602353907455755'; my @arr = unpack '(a2)*', $string; print qq{@arr}; " 86 02 35 39 07 45 57 55


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

Re: Pulling bytes out of string
by toolic (Bishop) on Oct 21, 2015 at 19:08 UTC

      Sure looks to me that the h (or H) formats are just what the doctor ordered.   (FYI:   the format-strings are defined, in very great detail, in perldoc pack.)

        See also perlpacktut.


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

Re: Pulling bytes out of string
by AnomalousMonk (Archbishop) on Oct 22, 2015 at 00:36 UTC

    It's unclear to me if mike65535 wants to unpack the example string given in the OP as pairs of characters to yield an array
        ('86', '02', '35', '39', ...)
    or, interpreting each character pair as a hex byte, to produce
        (0x86, 0x02, 0x35, 0x39, ...)
    or maybe something else. Can the OPer please clarify?

    Update: Oh, what the heck... Here's an example of the latter to go with the example of the former given above:

    c:\@Work\Perl\monks>perl -wMstrict -MData::Dumper -le "my $string = '8602353907455755'; my @arr = unpack 'C*', pack 'H*', $string; print Dumper \@arr; " $VAR1 = [ 134, 2, 53, 57, 7, 69, 87, 85 ];
    Of course, the dump is in decimal, so 0x86 == 134, etc. See Data::Dumper.


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

      Thanks for all the snippets!

      To the request that I "clarify" - I realize it's bad form but I'm not sure in what form I want the data.

      My first guess is any of the techniques will generate useful output for me, but I'm sure one will work better. ;-)

      Thanks again.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1145582]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (2)
As of 2024-04-20 05:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found