Search code examples
c++character-encodingasciifstream

Is 1857 an another character code for A?


The following statement prints 'A'. Why? Isn't 65 the ASCII code for 'A'?

cout<<char(1857);

Is 1857 getting converted into 65 with the cast? I am reading characters from a fstream using int_type get() method and getting weird values like 1857 for 'A', 1858 for 'B' and so on..

This is my code:

int ch;
while(file) // file is fstream.
{
  ch = file.get();
   cout<<char(ch)<<":"<<ch<<" ";  /*prints A:1857 B:1858 C:1859*/
}

Note: Casting int to char is truncating it to 8 bits and thus 1857 is converted into 65. But why is get() method returning 1857 instead of 65?


Solution

  • 1857 in binary 0111 0100 0001
    

    Char truncates that to lower 8 bits which is 0100 0001 the binary of which is 65, the character 'A'