#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Victoria, Australia
    Posts
    6
    Rep Power
    0

    Reading bits from a binary file


    Gday All,

    I am having a bit of trouble trying to work how to read bits out of a binary dat file.

    I need to be able to read bitstream stored in a .dat file and then display the bitstream in its raw form.

    Could someone please advise me on an easy way to code this?

    Any help would be great!
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    More often than not, a more experienced programmer can be confused by a more novice programmer's description of what he's trying to do. So please be patient with our stupid questions asking what you mean.

    Are you wanting to read each byte of a binary file and output those bytes' binary values? E.g., if the first byte contains a value of 0x4D you would want to output the ASCII string, "01001101"?
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Victoria, Australia
    Posts
    6
    Rep Power
    0

    Re: Reading bits from a binary file


    Gday dwise1_aol,

    Yes i want to do exactly that.
  6. #4
  7. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244

    Is this what you are looking for?


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void bytes2bits(unsigned char * dat, int len){
        unsigned char mask[] = {
            128,
            64,
            32,
            16,
            8,
            4,
            2,
            1
        };
        int i, j;
    
        for (i=0; i<len; i++){
            printf("ASCII value: %c\tint value: %u\tbinary value: ", dat[i], (unsigned char)dat[i]);
            for (j=0; j<8; j++){
                if (dat[i] & mask[j])
                    printf("1");
                else
                    printf("0");
            }
            printf("\n");
        }
    }
    
    int main(){
        unsigned char dat[] = "a bunch of data goes here!";
        bytes2bits(dat, sizeof(dat));
        return 0;
    }

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Victoria, Australia
    Posts
    6
    Rep Power
    0

    Thanks


    Thankyou

    That looks like it will help.

IMN logo majestic logo threadwatch logo seochat tools logo