help wanted reading strange external binary data (its format is given)

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
J G
Reply | Threaded
Open this post in threaded view
|

help wanted reading strange external binary data (its format is given)

J G
Hi, all,

When trying to read an external binary data file written by C(see attachment), I encounter this problem:
It's known that it contains rows of records with 36 bytes for a row and the first field of the each "row" in the file ought to be a yyyymmdd date. the field has 32 bits(4 bytes), but the first 12 bits of it represents yyyy while the next 4 bits mm and still next 5 bits dd, remain 11 bits not used. In fact:
#[0 24 116 124]  should read 19910403
I don't know how to operate on bits and read the integer out.

Here is the first 36 bytes of the data file. Except for the date (line 1), rest of it are read out correctly:

#[0 24 116 124]   2087983104
#[0 0 0 0]    0   
#[0 0 0 0]    0
#[0 0 0 0]    0
#[0 0 0 0]    0
#[0 0 0 0]    0
#[150 0 0 0]    150
#[68 0 0 0]    68
#[0 0 0 0]    0

Anyone has some advices? Thanks in advance!

Best Regards,

Jim G
Reply | Threaded
Open this post in threaded view
|

Re: help wanted reading strange external binary data (its format is given)

Reinout Heeck

On Nov 17, 2007, at 5:28 AM, Jim Guo wrote:
>
> I don't know how to operate on bits and read the integer out.
>

The basics are simple: use #bitAnd: to mask out the bits of interest  
and then #bitShift: these to normalize the resulting value.

Unfortunately this leads to rather dense (unreadable) code. So if you  
have to maintain a lot of these decoding methods you may be better  
off by implementing a bitstream first, then you can write more  
readable code like
   year := bits next: 11.


See also http://en.wikipedia.org/wiki/Mask_%28computing%29



HTH,

Reinout
-------

J G
Reply | Threaded
Open this post in threaded view
|

Re: help wanted reading strange external binary data (its format is given)

J G
In reply to this post by J G
My God I've failed to attach the original data file. It's here anyway.

2007/11/17, Jim Guo <[hidden email]>:
Hi, all,

When trying to read an external binary data file written by C(see attachment), I encounter this problem:
It's known that it contains rows of records with 36 bytes for a row and the first field of the each "row" in the file ought to be a yyyymmdd date. the field has 32 bits(4 bytes), but the first 12 bits of it represents yyyy while the next 4 bits mm and still next 5 bits dd, remain 11 bits not used. In fact:
#[0 24 116 124]  should read 19910403
I don't know how to operate on bits and read the integer out.

Here is the first 36 bytes of the data file. Except for the date (line 1), rest of it are read out correctly:

#[0 24 116 124]   2087983104
#[0 0 0 0]    0   
#[0 0 0 0]    0
#[0 0 0 0]    0
#[0 0 0 0]    0
#[0 0 0 0]    0
#[150 0 0 0]    150
#[68 0 0 0]    68
#[0 0 0 0]    0

Anyone has some advices? Thanks in advance!

Best Regards,

Jim G



--
Best Regards,

Jim G

000001.wgt (590 bytes) Download Attachment