Convert string to binary

Discussion in 'C++' started by Matej Barac, Jan 31, 2005.

  1. Matej Barac

    Matej Barac Guest

    I want to read a file then save it's binary data as a .txt with one's and
    zero's representing the actual file's bits. So it could be later
    reconstructed from that txt file. As of yet, I read the file into a string.

    I've tried something with bitset but I can't seem to have it read a string.
     
    Matej Barac, Jan 31, 2005
    #1
    1. Advertising

  2. Matej Barac

    David Harmon Guest

    On Mon, 31 Jan 2005 03:14:12 +0100 in comp.lang.c++, Matej Barac
    <> wrote,
    >I want to read a file then save it's binary data as a .txt with one's and
    >zero's representing the actual file's bits. So it could be later
    >reconstructed from that txt file. As of yet, I read the file into a string.
    >
    >I've tried something with bitset but I can't seem to have it read a string.


    What happened when you tried?
    Where is your code?

    This issue is covered in Marshall Cline's C++ FAQ. See the topic
    "[5.8] How do I post a question about code that doesn't work
    correctly?" It is always good to check the FAQ before posting. You
    can get the FAQ at:
    http://www.parashift.com/c -faq-lite/

    Compare:

    #include <iostream>
    #include <iterator>
    #include <bitset>
    using namespace std;
    int main()
    {
    std::copy(istream_iterator<char>(cin),
    istream_iterator<char>(),
    ostream_iterator<bitset<8> >(cout, "\n"));
    }
     
    David Harmon, Jan 31, 2005
    #2
    1. Advertising

  3. Matej Barac

    Matej Barac Guest

    On Mon, 31 Jan 2005 02:30:26 GMT, David Harmon wrote:

    > On Mon, 31 Jan 2005 03:14:12 +0100 in comp.lang.c++, Matej Barac
    > <> wrote,
    >>I want to read a file then save it's binary data as a .txt with one's and
    >>zero's representing the actual file's bits. So it could be later
    >>reconstructed from that txt file. As of yet, I read the file into a string.
    >>
    >>I've tried something with bitset but I can't seem to have it read a string.

    >
    > What happened when you tried?
    > Where is your code?
    >
    > This issue is covered in Marshall Cline's C++ FAQ. See the topic
    > "[5.8] How do I post a question about code that doesn't work
    > correctly?" It is always good to check the FAQ before posting. You
    > can get the FAQ at:
    > http://www.parashift.com/c -faq-lite/
    >
    > Compare:
    >
    > #include <iostream>
    > #include <iterator>
    > #include <bitset>
    > using namespace std;
    > int main()
    > {
    > std::copy(istream_iterator<char>(cin),
    > istream_iterator<char>(),
    > ostream_iterator<bitset<8> >(cout, "\n"));
    > }


    Sorry, but as I said I don't have any (working) code except the read/write
    file part. I have some trouble with turning a string into a bitset.

    Shouldn't the below work?

    string input; // read from input file
    bitset<10> bit((string) input);
    cout << bit;

    Compiles but produces a runtime error.
     
    Matej Barac, Jan 31, 2005
    #3
  4. David Harmon wrote:
    > std::copy(istream_iterator<char>(cin),
    > istream_iterator<char>(),
    > ostream_iterator<bitset<8> >(cout, "\n"));


    Note that this will remove whitespaces or, more precisely, the bits
    representing them: 'std::istream_iterator<T>' uses the formatted input
    operations to read the data. By default, the formatted input functions
    skip leading whitespace, even if they just read a single character.
    Also, there is no guarantee that 'char' has exactly 8 bits: the
    standard only guarantees that it has at least 8 bits. You might want to
    use this instead:

    /**/ std::copy(std::istreambuf_iterator<char>(std::cin)),
    /**/ std::istreambuf_iterator<char>(),
    /**/ std::eek:stream_iterator<
    /**/ std::bitset<std::numeric_limits<char>::digits>
    /**/ >(std::cout, "\n"));
    --
    <mailto:> <http://www.dietmar-kuehl.de/>
    <http://www.contendix.com> - Software Development & Consulting
     
    Dietmar Kuehl, Jan 31, 2005
    #4
  5. Matej Barac

    David Harmon Guest

    On Mon, 31 Jan 2005 04:33:29 +0100 in comp.lang.c++, Matej Barac
    <> wrote,
    >Shouldn't the below work?
    >
    >string input; // read from input file
    >bitset<10> bit((string) input);
    >cout << bit;


    A string is a sequence of characters. You want to convert the
    characters one by one, not the whole string at once.

    //(E.g. Using a clunky loop instead of std::copy)

    for(int i=0; i<input.size(); ++i) {
    cout << bitset<8>(input) << '\n';
    }
     
    David Harmon, Jan 31, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Achim Domma
    Replies:
    3
    Views:
    617
    Sjoerd Mullender
    Feb 17, 2004
  2. Eric
    Replies:
    3
    Views:
    1,070
    Bengt Richter
    Apr 22, 2004
  3. HNT20
    Replies:
    15
    Views:
    27,625
    blairg23
    Oct 8, 2009
  4. bussiere maillist

    convert a long string in binary

    bussiere maillist, Aug 20, 2006, in forum: Python
    Replies:
    3
    Views:
    543
    Maric Michaud
    Aug 22, 2006
  5. Harlin Seritt
    Replies:
    29
    Views:
    1,187
    Paul Rubin
    Feb 24, 2007
Loading...

Share This Page