I am trying to read data from a small binary file, byte by byte and then print each byte to console. Heres the code:
#include "stdafx.h"
#include <fstream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
ifstream snoop;
snoop.open("c:\\data.bin", ios::in | ios::binary);
char character1;
int counter;
counter=1;
while (counter<50)
{
snoop.get(character1);
cout << "\nAscii character: " << character1;
cout << " hexadecimal rep: " << hex << int(character1);
cout << " decimal rep: " << dec << int(character1);
counter=counter+1;
}
return 0;
}
Its supposed to print the ascii character equivalent and then the actual ascii code number in hex and decimal. Problem is that the type char doesnt go upwards of 128 in value so any byte that coincidentally happens to be between 129 and 255 in value will go negative because type char is from -128 to +128. I recognised this problem but my new problem is that get() doesnt work on unsigned chars.
#include "stdafx.h"
#include <fstream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
ifstream snoop;
snoop.open("c:\\data.bin", ios::in | ios::binary);
char character1;
int counter;
counter=1;
while (counter<50)
{
snoop.get(character1);
cout << "\nAscii character: " << character1;
cout << " hexadecimal rep: " << hex << int(character1);
cout << " decimal rep: " << dec << int(character1);
counter=counter+1;
}
return 0;
}
Its supposed to print the ascii character equivalent and then the actual ascii code number in hex and decimal. Problem is that the type char doesnt go upwards of 128 in value so any byte that coincidentally happens to be between 129 and 255 in value will go negative because type char is from -128 to +128. I recognised this problem but my new problem is that get() doesnt work on unsigned chars.