I want to read in a set of 256 bytes from a binary (.bin) file and store it into a buffer. Then I want to move each of those bytes into a vector of char vectors. Also, I'd like to read the char values in the vector and get the corresponding integer value of the binary value for that byte. This is how I've approached it:
typedef unsigned char BYTE;
vector< vector<BYTE> > pMem (256, vector<BYTE> (256, '0'));
ifstream binStream;
binStream.open("BACKING_STORE.bin", ios::binary | ios::in );
// allocate memory:
char * buffer = new char [256];
binStream.seekg(0, binStream.beg);
binStream.read(buffer, 256);
for( i = 0; i < 256; i++ )
pMem[0][i] = buffer[i];
for( i = 0; i < 256; i++ )
{
cout << "Physical memory char contents at 0[" << i << "]: " << pMem[0][i] << endl;
int x = pMem[0][i];
cout << "Physical memory integer contents at 0[" << i << "]: " << x << endl;
}
But when I print out the values, I get results like:
Physical memory char contents at 0[237]:
Physical memory integer contents at 0[237]: 0
Physical memory char contents at 0[238]:
Physical memory integer contents at 0[238]: 0
Physical memory char contents at 0[239]: ;
Physical memory integer contents at 0[239]: 59
Physical memory char contents at 0[240]:
Physical memory integer contents at 0[240]: 0
Physical memory char contents at 0[241]:
Physical memory integer contents at 0[241]: 0
Physical memory char contents at 0[242]:
Physical memory integer contents at 0[242]: 0
Physical memory char contents at 0[243]: <
Physical memory integer contents at 0[243]: 60
Physical memory char contents at 0[244]:
Physical memory integer contents at 0[244]: 0
Physical memory char contents at 0[245]:
Physical memory integer contents at 0[245]: 0
Physical memory char contents at 0[246]:
Physical memory integer contents at 0[246]: 0
Physical memory char contents at 0[247]: =
Physical memory integer contents at 0[247]: 61
Physical memory char contents at 0[248]:
Physical memory integer contents at 0[248]: 0
Physical memory char contents at 0[249]:
Physical memory integer contents at 0[249]: 0
Physical memory char contents at 0[250]:
Physical memory integer contents at 0[250]: 0
Physical memory char contents at 0[251]: >
Physical memory integer contents at 0[251]: 62
Physical memory char contents at 0[252]:
Physical memory integer contents at 0[252]: 0
Physical memory char contents at 0[253]:
Physical memory integer contents at 0[253]: 0
Physical memory char contents at 0[254]:
Physical memory integer contents at 0[254]: 0
Physical memory char contents at 0[255]: ?
Physical memory integer contents at 0[255]: 63
This is obviously the wrong output. Can someone please explain what I've done wrong and how accomplishing this might be done?
When you have:
char c = 'a';
std::cout << c << std::endl;
the output will be
a
When the value of c
corresponds to one of non-printing characters, the output varies from system to system. In your case you are getting blank output.
If you want the integer representation of 'a'
to be printed, you have to cast it.
std::cout << (int)c << std::endl;
or
int x = c;
std::cout << x << std::endl;
I hope that's all the explanation you need.