byte to bit & bit to byte

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mb29
    New Member
    • Jun 2007
    • 3

    #1

    byte to bit & bit to byte

    i have been working on this problem for last 2 days but i think i need someones help

    so here's my problem . . .

    i want to read a character from a file, convert that into a string of 0's n 1's; store them into an array binary[8] and write them to another file.

    then read the second file which contained the binary code of the file an convert it back to its exact original form . . . . but how?

    n one last question what does this mean a bit or a byte :

    fstream cfile("abc.dat" );
    char O = ' 0 ';
    cfile<<O-48;
    cfile.close();

    I m working on c++ 3.0 compiler
    Last edited by mb29; Jun 26 '07, 09:06 AM. Reason: forgot to mention compiler version
  • DeMan
    Top Contributor
    • Nov 2006
    • 1799

    #2
    a bit literally stands for "binary digit" and is basically a single digit in binary (a 1 or a 0). A Byte (look up in wikipedia, there is some debate over the exact origins of the word) is a series of bits (these days, usually 8) which are grouped together to represent something (eg with 8 bits to a byte we have 2^8 different arrangements - 0 to 255, which can be represented by the ascii characters).

    It is not totally clear to me what you want to do, but it sounds like you want to convert a character to it's bit string. A character is simply a one-byte integer, and can (mathematically ) be treated as such.

    One way you could get this value is (in pseudocode)
    Code:
    char b = <the caracter you want>;
    string bin="";
    for(int i=0; i<8; i++)
    {
      bin = (b mod 2) + bin;
      b=b/2;
    }
    if you have a string of characters, just iterate through each character and repeat this for each one....

    Comment

    • mb29
      New Member
      • Jun 2007
      • 3

      #3
      ok thank u for ur reply.
      let me explain my question once again to you

      i want to read a character from a file (fstream, xyz.ext), convert that into a string of 0's n 1's(either integers or char's anyone); store them into an array int binary[8] and write them to another file.

      after that again read the binary[8] data from the file(abc.dat) which we wrote in the first step and again convert that back into the original file( that was xyz.ext)

      Comment

      • DeMan
        Top Contributor
        • Nov 2006
        • 1799

        #4
        to convert back from the bit string above we can do something like

        Code:
        char bin[] = <the binary string from before>
        char val = 0;
        for(int i=0; i<8 i++)
        {
          val = val*2;
          val = val + bin[i];
        }

        Comment

        • pntkiran
          New Member
          • Jun 2007
          • 16

          #5
          Originally posted by DeMan
          to convert back from the bit string above we can do something like

          Code:
          char bin[] = <the binary string from before>
          char val = 0;
          for(int i=0; i<8 i++)
          {
            val = val*2;
            val = val + bin[i];
          }
          You want to read character from file ..mean you read "c" , then u want to convert into binary. " 1100011 " and then want to store it into int binary[8] = { 1,1,0,0,0,1,1} ; and then want to write binary data into other file.

          and then after u want to do reverse of that ...Wrong then correct me ..

          .
          Kiran Parmar

          Comment

          • mb29
            New Member
            • Jun 2007
            • 3

            #6
            Originally posted by pntkiran
            You want to read character from file ..mean you read "c" , then u want to convert into binary. " 1100011 " and then want to store it into int binary[8] = { 1,1,0,0,0,1,1} ; and then want to write binary data into other file.

            and then after u want to do reverse of that ...Wrong then correct me ..

            .
            Kiran Parmar
            yes u are right. if u can help me then plsease tell me further step in better way

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              Don't overlook using the STL:

              [code=cpp]
              int data = 11234;
              bitset<32> bits(data);
              string onesandzeroes = bits.to_string( );
              [/code]

              and you are done.

              Comment

              Working...