S
sas
I have a problem printing cyrillic text to stdout in Linux. I know
that it has to be UTF-8. I'm trying to read a symbol, guess that it is
cyrillic encoded as CP1251, and if so output it as cyrillic in UTF-8,
but my code so far doesn't work.
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
int
main()
{
if (!setlocale(LC_CTYPE, "")) {
fprintf(stderr, "Can't set the specified locale! "
"Check LANG, LC_CTYPE, LC_ALL.\n");
return 1;
}
while (!feof(stdin)) {
wchar_t c = fgetc(stdin);
// 'á'-'ñ'
if (c >= 0xc0 && c <= 0xdf)
{
c -= 0xc0;
c += 0x410;
}
// 'Á'-'Ñ'
if (c >= 0xe0 && c <= 0xff)
{
c -= 0xe0;
c += 0x430;
}
printf("%lc", c);
}
return 0;
}
that it has to be UTF-8. I'm trying to read a symbol, guess that it is
cyrillic encoded as CP1251, and if so output it as cyrillic in UTF-8,
but my code so far doesn't work.
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
int
main()
{
if (!setlocale(LC_CTYPE, "")) {
fprintf(stderr, "Can't set the specified locale! "
"Check LANG, LC_CTYPE, LC_ALL.\n");
return 1;
}
while (!feof(stdin)) {
wchar_t c = fgetc(stdin);
// 'á'-'ñ'
if (c >= 0xc0 && c <= 0xdf)
{
c -= 0xc0;
c += 0x410;
}
// 'Á'-'Ñ'
if (c >= 0xe0 && c <= 0xff)
{
c -= 0xe0;
c += 0x430;
}
printf("%lc", c);
}
return 0;
}