CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Aug 2009
    Posts
    6

    Converting from string("01000001") to a byte and back.

    AHHHHH! I give up. How do I convert a strings of ones and zeros into a byte that I can write to a binary file??? I've tried everything I can think of. Here is what i have so far:

    Code:
        string val = "01000001"; //Binary for letter 'A'
        unsigned char a = 00000000;
        //cout << a << endl;
        unsigned char MASK = 1 << 0;
        //cout << MASK << endl;
        
        for (int i = val.size()-1; i >= 0; i--) {
            a <<= 1;
            if(val.substr(i,i+1)=="1"){
                a |= MASK;
            }
        }
        cout << a << endl;
    fyi i want to write the file like this, so i need it in char arrays.

    char * output; //load data, etc. etc.
    writeFile(output, length, "output.txt");

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Converting from string("01000001") to a byte and back.

    If you know precisely how many bits you're expecting to have, you can use a std::bitset to convert between string and unsigned long representations. This will only work if you have fewer than the number of bits in an unsigned long, of course.

    Converting further to an unsigned char will require a static_cast. You could use a boost::numeric_cast if you wanted to check that you aren't losing data via the downcast.

  3. #3
    Join Date
    Aug 2009
    Posts
    6

    Re: Converting from string("01000001") to a byte and back.

    Thanks. Here is what I got for converting back and forth between strings and unsigned chars.

    Code:
        string myString1 = "01001010";
        cout << myString1 << endl;
        bitset<8> bitset1(myString1);
        unsigned char a = static_cast<unsigned char>(bitset1.to_ulong());
        cout << a << endl;
        unsigned long l = static_cast<unsigned long>(a);
        bitset<8> bitset2(l);
        string myString2(bitset2.to_string<char,char_traits<char>,allocator<char> >());
        cout << myString2 << endl;
    Works great for me.

    Btw, is there an easy interface to hold variable length bits and append them together? Or will I simply have to use strings?

    Moreover, is there an easy way to write said bits to a file one a time? Or will I have to manually pad them into groups of 8? (Or is it 16?<--Not sure on this).

    Thank you so much for your help.

    -OrangeKyo

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Converting from string("01000001") to a byte and back.

    You'll have to pad them into groups of 8 bits in order to write them to a file. No APIs I know of operate on a resolution lower than a byte.

    Given that requirement, you could concatenate them as a string of chars (each being 8 bits) rather than a string of 1s and 0s (each being a character, and thus 64x less space-efficient).

  5. #5
    Join Date
    Aug 2009
    Posts
    6

    Re: Converting from string("01000001") to a byte and back.

    Ah but I require variable length bits. A string of char won't do. Each char is 8 bits long only.

    (When I write to the file I will chunk them into 8 and convert to char naturally.)

    Thanks a bunch for your help. I think I have what i need now.

    - OrangeKyo

  6. #6
    Join Date
    Feb 2009
    Posts
    326

    Re: Converting from string("01000001") to a byte and back.

    Not sure this would help:

    I have printed the binary bits of a char
    Then done the same with a char* (or a char array)

    Given below is the program, see if it helps:
    Code:
    #include <iostream>
    using std :: cout;
    using std :: endl;
    
    void printInBinary(const unsigned int pUnsignedNumber, const unsigned int pSize_in_bytes);
    void processWord(const char* pCharPtr);
    
    union UnionDataTypeSet
    {
        unsigned int unsignedNumber;
        char character;
    };
    
    
    int main()
    {
        char *word = "Happy";
    
        system("clear");
    
        processWord(word);
            
        return(0);
    }
    
    void printInBinary(const unsigned int pUnsignedNumber, const unsigned int pSize_in_bytes)
    {
        const unsigned short int SIZE_OF_TYPE_IN_BITS = pSize_in_bytes * 8;
    
        for(short int index = SIZE_OF_TYPE_IN_BITS - 1; index >= 0; index --) 
        {   
            unsigned short int BIT = (pUnsignedNumber >> index) & 1;
            cout << BIT;
        }   
    
    }
    
    void processWord(const char* pCharPtr)
    {
        unsigned int index = 0;
        UnionDataTypeSet dataTypeSet1;
    
        while(pCharPtr[index] != '\0')
        {   
            dataTypeSet1.character = pCharPtr[index];
            printInBinary(dataTypeSet1.unsignedNumber, sizeof(char));
            cout << endl; //Just to distinguish the letters
            index++;
        }   
    }

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured