How to Make Cout Behave as in Binary Mode

How to print (using cout) a number in binary form?

The easiest way is probably to create an std::bitset representing the value, then stream that to cout.

#include <bitset>
...

char a = -58;
std::bitset<8> x(a);
std::cout << x << '\n';

short c = -315;
std::bitset<16> y(c);
std::cout << y << '\n';

format binary literals with std::cout

Here is one of solutions:

template<size_t I>
std::ostream& operator << (std::ostream& os, const bitset<I>& b)
{
int i;
int check = b.size()-b.size()%3;

for(i=b.size()-1; i >= check; --i)
os << b[i];

if(i>0)
os << char(39);

for(; i>=0; --i)
{
os << b[i];
if(i%3 == 0 && i!=0)
os << char(39);
}
return os;
}

c++ cout instead of fstream

First, there is absolutely no reason to use different code for a std::fstream and for std::cout (Beside which, your cout-code uses formatted output, and wrong):

You are using their ostream-interface in both cases.

So, first things first, repair the second code (as far as possible) by replacing it with the first.


Now, we come to the one difference (which you tried to paper over with setf): std::cout is in text-mode, not binary mode!

Unfortunately, none of ios::out, ios::binary and ios::trunc are formatting-flags, so you cannot set them with setf.

Actually, the mode cannot be changed at all after the fact (at least not portably).

Fortunately, you can simply ignore having the wrong mode on many systems, as Linux and others equate textmode and binary-mode. On windows, this hack should get you around it:

cout.flush();
fflush(stdout);
_setmode(_fileno(stdout), _O_BINARY);

C++ Binary Read Write Files

For writing binary files, use std::ostream::write() method, not operator<<:

FileCreator.write((char *) &AccountNumber, sizeof(AccountNumber));

The cast is necessary because there is no overload for writing integers to the stream.

Remember, read and write are paired for binary I/O.

Edit 1: Fixed length & variable length records
Be aware that you need the size of the item when writing and reading. This will work for fixed size/length data items and structures. However, it does not work well with variable length data, such as text.

For variable length records, you may want to write the length first followed by the data:

static const char hello[] = "Hello";
static const unsigned int data_size(sizeof(hello) - 1);
FileCreator.write((char *) &data_size, sizeof(data_size));
FileCreator.write(&hello[0], data_size);

In the above example, the "- 1" is there so that the terminating NUL character is not written to the file. You don't need this for binary files, but YMMV (I use the idiom when writing to console and other human readable streams).

Printing in binary format (user defined function)

The problem is your input data. You are providing 2 or 0 as a character, not as a number.

Let me explain:

  • character '2' -> ASCII 0x32, 32 is the real input of your function, 00110010 is the bit representation of 0x32.
  • character '0' -> ASCII 0x30 has 00110000 as binary representation.

Hence the function above is working.

In order to solve the issue look at the way you are collecting the input data.

My 2 cents, Ste.



Related Topics



Leave a reply



Submit