Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Extraction of bits using unpack

Reply
Thread Tools

Extraction of bits using unpack

 
 
pip
Guest
Posts: n/a
 
      08-17-2005
Hi all,

I am trying to extract some data from a binary file using unpack. All
is well where I have to extract simple data types such as shorts,
strings and ints but then I reach this in the docs I am using:

Here is the way a date is represented:

MSB LSB
__________________________________________________ ________
| year | Month | Day | Time |
| 12 bits | 4 bits | 5 bits | 11 bits |
|__________|____________|___________|_____________ _______|

I have played with the hex string operator in unpack with little
joy. I also had a go at it with vec() but got nowhere.

Could someone please point me in the right direction for extracting
this data please?

Thanks,

pip

P.S if it makes a difference I am pulling the file from an x86 based
system and parsing it on an x86 system.
 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      08-17-2005
pip <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> Hi all,
>
> I am trying to extract some data from a binary file using unpack. All
> is well where I have to extract simple data types such as shorts,
> strings and ints but then I reach this in the docs I am using:
>
> Here is the way a date is represented:
>
> MSB LSB
> __________________________________________________ ________
> | year | Month | Day | Time |
> | 12 bits | 4 bits | 5 bits | 11 bits |
> |__________|____________|___________|_____________ _______|
>
> I have played with the hex string operator in unpack with little
> joy. I also had a go at it with vec() but got nowhere.
>
> Could someone please point me in the right direction for extracting
> this data please?


You want the usual shifting and masking of integers, it seems. Get
the four bytes into an integer $x (unpack), and apply

my( $year, $month, $day, $time) = bit_split( $x, 12, 4, 5, 11);

with

sub bit_split {
my $bits = shift;
my @parts;
for ( reverse @_ ) {
my $mask = ( 1 << $_) - 1;
unshift @parts, $mask & $bits;
$bits >>= $_;
}
@parts;
}

Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
 
Reply With Quote
 
 
 
 
pip
Guest
Posts: n/a
 
      08-17-2005
Anno,

> pip <(E-Mail Removed)> wrote in comp.lang.perl.misc:
>> Hi all,
>>
>> I am trying to extract some data from a binary file using unpack. All
>> is well where I have to extract simple data types such as shorts,
>> strings and ints but then I reach this in the docs I am using:
>> [ ... snip ... ]

>
> my( $year, $month, $day, $time) = bit_split( $x, 12, 4, 5, 11);
>
> with
>
> sub bit_split {
> my $bits = shift;
> my @parts;
> for ( reverse @_ ) {
> my $mask = ( 1 << $_) - 1;
> unshift @parts, $mask & $bits;
> $bits >>= $_;
> }
> @parts;
> }


This worked a charm, I would never have worked this out without your
help. Now to go a read up on this bit shifting malarkey.

Cheers,

pip
 
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
sum of 64 bits using 32 bits cpu sarahh C Programming 18 05-14-2008 02:56 PM
8 bits/ch vs 16 bits/ch in PS Terry Digital Photography 5 01-21-2004 06:59 PM
8-Bits vs 12 or 16 bits/pixel; When does more than 8 bits count ? Al Dykes Digital Photography 3 12-29-2003 07:08 PM
win XP 32 bits on a 64 bits processor.. Abbyss Computer Support 3 11-13-2003 12:39 AM
pack/unpack bits Aaron Brice Perl Misc 1 08-06-2003 03:35 AM



Advertisments