in reply to Re: Handling Hex data with Dynamic unpack
in thread Handling Hex data with Dynamic unpack

Some how perl doesn't allow me to unpack like this (look at H2 H2 H16 H2 A2 ...) :
my @stack = unpack q{ H2 ### Header Record Id : 1 Byte H2 ### File Format Version : 1 Byte H16 ### Timestamp : 8 Bytes H2 ### No. of BSCs : 1 Byte ### For each BSC ... A2 ### BSC Id : 1 Byte A2 ### Application Version : 1 Byte A4 ### BSC Name : 2 Bytes A4 ### Number of Cells : 2 Bytes ### For each Cell ... A4 ### Cell Pointer : 2 Bytes A18 ### Cell Name : 9 Bytes A4 ### Number of Neighbour Cells : 2 Bytes ### For each Neighbour Cell to this BSC ... A4 ### Cell Pointer : 2 Bytes A18 ### Cell Name : 9 Bytes }, $rawAsHex;

Any idea how this could be done?

I get a output like this :

[ 48, 48, "3031303030303030", [ 30, 30, "3031303030303030", 30, "04", "f9", "14f1", "c0b0", 1004, "253434130300000000", 5000, "0434", "130303030300000000", ], ]

Replies are listed 'Best First'.
Re^3: Handling Hex data with Dynamic unpack
by Anonymous Monk on Jul 05, 2012 at 11:38 UTC

    Some how perl doesn't allow me to unpack like this (look at H2 H2 H16 H2 A2 ...) ... Any idea how this could be done?

    I showed you how. What you're dealing is a text-strings, so if you want bytes, you have to pack them. First pack them to get bytes ( pack 'H*' ) then pack them to get what you're really after ( C An unsigned char (octet) value. ) .....

    Commands

    perl -le " print unpack q{H*}, q{Y} " perl -le " print pack q{H*}, q{59} " perl -le " print ord q{Y} perl -le " print pack q{H*}, q{59} " perl -le " print unpack q{C}, pack q{H*}, q{59} "

    Session

    $ perl -le " print unpack q{H*}, q{Y} " 59 $ perl -le " print pack q{H*}, q{59} " Y $ perl -le " print ord q{Y} 89 $ perl -le " print pack q{H*}, q{59} " Y $ perl -le " print unpack q{C}, pack q{H*}, q{59} " 89

    Y encoded as hex is 59

    The numeric value ( ord ) of Y is 89

    The C An unsigned char (octet) value, 8-bits, 1-byte of Y is 89

      Okie ... will try :)

      Thanks once again.