I have a binary file called "input.bin" where every character is of 4 bits. The file contains this kind of data:
0f00 0004 0018 0000 a040 420f 0016 030b
0000 8000 0000 0000 0000 0004 0018 0000
where 0f is the first byte.
I want to read this data and to do that, I am using the following code:
#include <string>
#include <iostream>
#include <fstream>
int main()
{
char buffer[100];
std::ifstream myFile ("input.bin", std::ios::in | std::ios::binary);
myFile.read (buffer, 100);
if (!myFile.read (buffer, 100)) {
std::cout << "Could not open the required file\n";
}
else
{
for (int i = 0; i < 4; i++)
{
std::cout << "buffer[" << i << "] = " << static_cast<unsigned>(buffer[i]) << std::endl;
}
myFile.close();
}
return 0;
}
Currently I am printing just four bytes of data, and when I run it, I get this output:
buffer[0] = 0
buffer[1] = 24
buffer[2] = 0
buffer[3] = 0
Why is it not printing the value of 0f and just printing the value of 18 in index 1 whereas it is actually at index 6?
<< buffer[i]--><< static_cast<int>(buffer[i])-- Then you actually see the decimal values, not box characters and blanks.