Printing extended ASCII in C

Asked

Viewed 670 times

2

Look at this simple 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 obtained: 102 97 110 116 195 161 115 116 105 99 97

The idea is to present the ASCII codes of each graphic signal. However, observing the ASCII table, the code for "á" should be 160. And it is displayed as 195 and 161. How can I resolve this situation so that "there" results in 160?

1 answer

5


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

The extended ASCII term is misused, and even criticized(en), because it can refer to a lot of different tables/encodings, and this goes even to Unicode, since its first 127 codepoints are the same as those of ASCII.

In short, there is no "extended ASCII table", it is 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 only to say that the original ASCII table is contained at the beginning of the charset used (such as the DOS code pages, the WIN-1252 sets, ISO-8859-1, UTF-8 (which is what you are using), etc.

  • In your specific case, is your code editor that is configured as UTF-8, and the solution would be to replace 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, then it would depend exclusively on the environment, and you would have to configure your system to the desired page.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.