I'm trying to read a binary file to get its individual bit values. I'm doing so using the following function.
string readFileBytes(const char *name)
{
ifstream fl(name);
fl.seekg( 0, ios::end );
size_t len = fl.tellg();
char *ret = new char[len+1];
fl.seekg(0, ios::beg);
fl.read(ret, len);
fl.close();
ret[len] = '\0';
return ret;
delete ret;
}
This works fine. For the next part I'm taking each element of the array ret
and using the following function I wrote to get the individual bit values.
string out;
for (int i=0; i < len; i++)
{
for (int x =7; x >=0; x--)
{
out += ((ret[i]>>x) & 0x01);
}
}
If I have cout << ((ret[i]>>x) & 0x01);
within the nested loop instead it prints the bit values perfectly. But when I have it as it is, and print the string (out
), I get nothing but smiley faces.
Any ideas why?