Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > bytes and characters - how to decode?

Reply
Thread Tools

bytes and characters - how to decode?

 
 
Panchal V
Guest
Posts: n/a
 
      08-21-2004
I have to process a packet, it looks like this :
$str = "\0\0\0\2\0\0\0\1\2\x41\x42";

version -> 2 (first 4 octects)
type -> 1 (next 4 octects)
length -> 1 (next 1 octect)
data -> 'AB' (next 2 octect)

how do i decode this packet??? i want first 4 bytes to be treated as
an Unsigned Integer i.e. version is 2 here in this case...

I tried
$str =~ m/(.{4})(.{4})(.)/;
$version = pack "I", $1;

but doesn't help... HOW CAN I extract fields values???

Thnx
-Neo
 
Reply With Quote
 
 
 
 
Tassilo v. Parseval
Guest
Posts: n/a
 
      08-21-2004
Also sprach Panchal V:

> I have to process a packet, it looks like this :
> $str = "\0\0\0\2\0\0\0\1\2\x41\x42";
>
> version -> 2 (first 4 octects)
> type -> 1 (next 4 octects)
> length -> 1 (next 1 octect)
> data -> 'AB' (next 2 octect)
>
> how do i decode this packet??? i want first 4 bytes to be treated as
> an Unsigned Integer i.e. version is 2 here in this case...
>
> I tried
> $str =~ m/(.{4})(.{4})(.)/;
> $version = pack "I", $1;
>
> but doesn't help... HOW CAN I extract fields values???


There is no need for the pattern match when you use unpack() properly.
It can return a list of values so the unpacking can be done in one go.
Assuming the above record is two unsigned integers, followed by a length
indicator being an unsigned char and finally that many characters, you'd
do it that way:

my ($ver, $type, $data) = unpack "IIC/a", $str;

Note that the 'I' template specifies integers in the native byteorder
and native width of your machine. Since "\0\0\0\2" only represents the
value 2 on big-endian machines, it is not portable to machines with a
different byteorder. Therefore you better use 'N':

my ($ver, $type, $data) = unpack "NNC/a", $str;

Tassilo
--
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus}) !JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexi ixesixeseg;y~\n~~dddd;eval
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
working set, virtual bytes and private bytes George2 C++ 1 01-09-2008 02:57 PM
An int that is 4 bytes in native, and 12 bytes in .NET? Huh? BogusException C++ 7 02-06-2007 04:47 AM
Re: receiving Bytes and sending Bytes Ieuan Adams Computer Support 0 07-24-2003 07:46 PM
Re: receiving Bytes and sending Bytes The Old Sourdough Computer Support 0 07-23-2003 01:23 PM



Advertisments