Printing ASCII extended in C

2

Look at this simpes code written in C:

unsigned char *palavra = "fantástica";
int tamanho = strlen(palavra);
int i;
for (i = 0; i < tamanho; i++)
    printf("%i ", palavra[i]);
printf("\n");

Output fetched: 102 97 110 116 195 161 115 116 105 99 97

The idea is to display the ASCII codes of each graphic signal. However, looking at the ASCII table, the code for "á" should be 160. And it is displayed as 195 and 161. How can I solve this situation, so that "á" will result in 160?     

asked by anonymous 11.06.2016 / 06:45

1 answer

5

The American Standard Code for Information Interchange (ASCII) goes up to character 127 only.

The extended ASCII term is poorly used, and even criticized > , because it can refer to a bunch of different tables / encodings, and this holds true even for Unicode, since its first 127 codepoints are the same as ASCII.

In short, there is no "extended ASCII table", it's just a generic term.

To give 160, it only depends on the codepage you are using (ex: CP850 and CP437). The "guys" use this name just to say that the original ASCII table is contained at the beginning of the charset used (such as DOS code pages, WIN-1252, ISO-8859-1, UTF-8 sets (which is what you are using), etc.

  • In your specific case , it is your code editor that is configured as UTF-8, and the solution would be to change its encoding when creating / saving the file. A good test would be to save using a CMD or System Shell editor.

  • If it were an input for the user to type, it would depend exclusively on the environment, and you would have to configure your system for the page you want.

11.06.2016 / 13:34