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

Posted on 2004-10-21
Last Modified: 2008-03-03
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.
Question by:Tertioptus
    LVL 55

    Expert Comment

    by:Jaime Olivares
    Have a look to this class, maybe could be helpful:
    Good luck,

    Expert Comment

    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;

    typedef struct 10BITVAL
       unsigned char bit1  :1;
       unsigned char bit10 :1;

    typedef struct ODDSIZEVALUE
           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.
    LVL 5

    Author Comment

    Im sorry.  I'm looking for c# code.
    LVL 55

    Accepted Solution

    Have you seen the BitArray class from .Net framework?

    it is located in the System.Collections namespace.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    Product Review - Android Remix

    Come along for the ride with our Senior Product Manager, Brian Matis, as he reviews the Android Remix.

    Suggested Solutions

    Title # Comments Views Activity
    Safe way to use a password in a bat or cmd file? 8 43
    find a node in VST 2 21
    Fibonacci challenge 11 45
    powerN  challenge 3 19
    Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
    This is about my first experience with programming Arduino.
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    875 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now