How to unpack strings?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ft_ml
    New Member
    • Jul 2006
    • 1

    How to unpack strings?

    Hello all,

    I´m trying to unpack variable length strings.

    In an application I receive a stream of bytes which contains a number n (2 bytes) and n strings of variable length. All strings ends whith a null.

    I wrote the next piece of code:

    sub GetStrings() {
    my ($data) = @_;

    ($num_of_string s) = unpack "n", $data;

    my $fmt = "n";
    $fmt .= "a*x" x $num_of_strings ;

    my @data_array= unpack $fmt, $data;
    }

    However it does not work!.

    I have been doing some trials and It seems that unpack sees the n strings as a unique string and it needs the exact length of it. For instance

    @data_array = unpack "na${length }x" $data

    do the job if $length has the sum of lengths of the strings.

    Can someone give some advice or explain the rigth way to do it?


    Thanks a lot.

    Carlos
Working...