Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Handling Hex data with Dynamic unpack

by Anonymous Monk
on Jul 05, 2012 at 11:06 UTC ( [id://980023]=note: print w/replies, xml ) Need Help??


in reply to Handling Hex data with Dynamic unpack

This is how I would start, but I'd need more complete spec

#!/usr/bin/perl -- use strict; use warnings; use Data::Dump; my $rawAsHex = q'0001000000004f914f1c0b0100425343413030000000050000434 +130303030300000000143413030303134000000024341303030323200000003434130 +3030323400000004434130303032390000000100224e43656c6c30000000020042534 +341303100000005000543413031303031000000064341303130303400000007434130 +31303130000000084341303130313500000009434130313031360000000100224e436 +56c6c30000000030042534341303200000006000a'; my $raw = pack 'H*', $rawAsHex; my @stack = unpack q{ A2 ### Header Record Id : 1 Byte A2 ### File Format Version : 1 Byte A16 ### Timestamp : 8 Bytes A2 ### 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; my $id = unpack 'C*', pack 'H*', $stack[0]; my $version = unpack 'C*', pack 'H*', $stack[1]; my $time = unpack 'H*', pack 'H*', $stack[2]; ## WHAT?! dd [ $id, $version, $time , \@stack ]; __END__ [ 0, 1, "000000004f914f1c", [ "00", "01", "000000004f914f1c", "0b", "01", "00", 4253, 4341, 3030, "000000050000434130", 3030, 3030, "000000014341303030", ], ]

Replies are listed 'Best First'.
Re^2: Handling Hex data with Dynamic unpack
by PerlJedi (Novice) on Jul 05, 2012 at 11:17 UTC

    Hey Thanks Anonymous Monk ... It certainly is a good starting point. I appretiate your replies :)

    Thanks and Regards,

    PerlJedi

Re^2: Handling Hex data with Dynamic unpack
by PerlJedi (Novice) on Jul 05, 2012 at 11:28 UTC
    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", ], ]

      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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://980023]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2024-04-19 12:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found