[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 209
  • Last Modified:

Reading data, of which bit sizes are not multiples of 8.

Ok, I am familiar with the BinaryReader class, and it's plethora of supported data formats

yet it is still very limited to what I need to do.

EXPERTS, what do you think is the best way, in .Net, to read and write 9,10,11 and especially 12 bit data.


My first inclination seems like a hack.  Where I would read data in 16-bit words, and perform bit masking on the data for each acquired value.  I'm sure that would work, but there's gotta be a better way.
0
Tertioptus
Asked:
Tertioptus
  • 2
1 Solution
 
Jaime OlivaresSoftware ArchitectCommented:
Have a look to this class, maybe could be helpful:
http://www.codeproject.com/cpp/XBitArray.asp
Good luck,
Jaime.
0
 
SuperCableManCommented:
Why not try using a bitfield or some fancy union? Then use a memcpy directly into the struct / union?

typedef struct EIGHTBITVAL
{
   unsigned char bit1 :1;
   unsigned char bit2 :1;
  ...
   unsigned char bit8 :1;
}EightBit_t;

typedef struct 10BITVAL
{
   unsigned char bit1  :1;
   ...
   unsigned char bit10 :1;
}TenBit_t;

typedef struct ODDSIZEVALUE
{
    union
    {
       struct 8
       {
           EightBit eight;
       }
       struct 10
       {
           TenBit_t  ten;
        };
       ....
       unsigned char workWithThisOne;
    }
}

Struct'ing it out like that might help.  Using that char at the end of the union will allow you to do whatever math you need to do, then switch between the types (because of the union) as necesary.  You may have some more overhead in keeping track of what size (8,9,10 bit value) but i hope that helps.
0
 
TertioptusAuthor Commented:
Im sorry.  I'm looking for c# code.
0
 
Jaime OlivaresSoftware ArchitectCommented:
Have you seen the BitArray class from .Net framework?

it is located in the System.Collections namespace.
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now