0

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?

Victor Brunell
  • 5,668
  • 10
  • 30
  • 46

1 Answers1

1

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.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • I tried the second method using: int x = pMem[0][i]; cout << "Physical memory integer contents at 0[" << i << "]: " << x << endl; But that doesn't seem to work. I also tried int x = (int)pMem[0][i]; but that gives the same result as I posted. – Victor Brunell Apr 11 '15 at 04:31
  • @Caulibrot, I don't see anything else wrong in your code. I can't explain why it doesn't work. Also, I don't know why you think it doesn't work. Please add to your post what is the expected output and what is the output you are getting. – R Sahu Apr 11 '15 at 04:35
  • Ah, I see where I went wrong now. The values are meant to be signed and I'm using unsigned char. Doh! Thanks for your help. – Victor Brunell Apr 11 '15 at 05:12