Why Not Printing Simple Character?

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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <fstream>

using namespace std;
int main()
{
    ifstream myFile;
    myFile.open("input.txt");
    char inputA;
    myFile>>inputA;
    cout<<inputA<<endl;
    cout<<(int)inputA;



}
"☼" is no ASCII symbol, it is UNICODE.

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)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <cstdlib>
#include <iostream>
#include <fstream>

using namespace std;

int main(int argc, char *argv[])
{
    fstream file ("input.txt");
    unsigned char 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";
    
    
    unsigned int 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;
}


I hope this may help a bit...
Last edited on
Topic archived. No new replies allowed.