I am reading a character from file '☼' this character is typed in notepad by pressing (ALT+15), now I have to print this character and the value 15(Respective ASCII value of this character) on console. The problem is I am getting a square box character with ASCII value -2. Why not this is property working?
if a character is in unicode and has a value higher than 127 (I assume you are working with a signed char) there could be a counter overflow and so you get the value -2 instead of something like 254, 510, 766 etc. (I suppose it is 15398 in this case)
#include <cstdlib>
#include <iostream>
#include <fstream>
usingnamespace std;
int main(int argc, char *argv[])
{
fstream file ("input.txt");
unsignedchar symbol[2]; //UNICODE uses 2 Bytes per character
file.ignore (2); // the first two bytes indicate the UNICODE format ( 0xFF FE)
file.read ((char*) symbol, 2);
file.close();
//output the bytes
cout << (int) symbol[0] << "\t" << (int) symbol[1] << "\n\n";
unsignedint value = 0; //buffer for interpreation
//High-endian interpretation
for (int i = 0; i < 2; i++) value += (symbol[i] << 8*i);
cout << dec << value << "\t" << hex << value << "\n\n";
value = 0;
//Low-endian interpretation
for (int i = 0; i < 2; i++) value += (symbol[i] << 8*(1-i));
cout << dec << value << "\t" << hex << value << endl;
system("PAUSE");
return 0;
}