0

Iam going through a book on applied c++

Binary string class is needed to collect and maniputlate binary streams of data. These data streams can represent anything from image data to the contents of objects. Here author also mentioned it is like object streaming or object persistance.

What does author mean by object stream. Can any one give me simple example on how we can write binary string class.

Thanks for your time and help.

4
  • Isn't that what fstream already is? Commented Jul 23, 2013 at 14:09
  • Look for serialization instead of object streaming Commented Jul 23, 2013 at 14:11
  • @CarlNorum: how fstream is related to object persistance Commented Jul 23, 2013 at 14:11
  • You can use it to keep persistent data between programs or instances. Commented Jul 23, 2013 at 14:12

2 Answers 2

1

Binary string class is needed to collect and manipulate binary streams of data.

This essentially means the string (or stream) can contain an arbitrary number of elements (binary data; bits), which could represent anything and you can change those by using the class.

These data streams can represent anything from image data to the contents of objects.

You can do with a stream whatever you want (usually only limited by implementation). For example, you can load raw image data into a stream object and then read single bytes. You could as well write single characters to a stream (or array) and save it as raw image data. The elemental string/stream doesn't limit you to any single interpretation. Instead the actual implementation/interface defines this part.

Here author also mentioned it is like object streaming or object persistance.

"Object streaming" here refers to transfering something. E.g. sending data over a network or port (e.g. using it as a buffer to send or receive data). "Object persistance" refers to keeping something (letting it persist) even while your program isn't running anymore (loading/saving).


Essentially, pretty much any stream as well as the containers available in the STL are already what the author considers a binary string. The difference is just the grouping, e.g. are you able to access/read/write single bits or just bytes/words/whatever?

To get a real "bit stream", you could just use something as simple as std::vector<bool>. This allows you to store boolean values, but you might as well read them as something different (e.g. reading them blockwise by casting the contents to an array of integers).

But I'd say something as simple as a std::stream would even better match the description of the author: It can be used to keep persistent data, it can be used to transfer data and it can be used to store/load data in different formats/interpretations. For example, you can write single bits or bytes and later on read them as an integer or even a string.

Sign up to request clarification or add additional context in comments.

1 Comment

Nitpick: [with std::stream] "you can write single bits or bytes" - no you can't - they operate in units of characters/bytes, though of course you can transfer a byte in which only one bit is of significance. Cheers
0

What does author mean by object stream.

They mean sending - from one part of a program - output that encodes the value of an object, such that the receiving code can construct an object with an equivalent value. It's pretty much like persistence (e.g. saving an object to disk so you can load it later), except that the data isn't necessarily put somewhere persistent (so it could be lost if the power was turned off) but it arrives at some other code that effectively does the reload step immediately.

For example, and without production levels of error checking:

struct X { int a; int b; };

std::ostream& operator<<(std::ostream& os, const X& x)
    { return os << x.a << ' ' << x.b; }

std::istream& operator>>(std::istream& is, X& x)
    { return is >> x.a >> x.b; }

X x1 = ...;
std::istringstream iss;
iss << x1;
X x2;
if (iss >> x)
    assert(x2 == x1);

Can any one give me simple example on how we can write binary string class.

You don't have to... the Standard includes std::string, which can handle binary data.

2 Comments

Even though std::string can indeed handle binary data, for the sake of semantics I prefer using std::vector<[unsigned ]char> for the task and reserve std::string for textual content.
@MatthieuM.: a worthy note, and quite a few people share your preference; personally I often lean towards std::string because it instantly communicates contiguous single-byte storage whereas with vector I have to pay attention to the data type, and it (controversially) has a far richer interface letting me do much more using member functions (which IMHO read better and are easier to find/use in most IDEs).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.