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.
LVL 5
TertioptusAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.