Re: attempting to print unicode characters.
- From: Ray <bear@xxxxxxxxx>
- Date: Sun, 29 Aug 2010 08:39:37 -0700
Ben Bacarisse wrote:
Ray <bear@xxxxxxxxx> writes:
Hi. I'm trying to print Unicode characters to standard output, and
failing.....
But I haven't been able to get wide-character output from a C
program.
Which is not what you want. You may use the wide output functions but
the output you want is multi-byte not wide. As has already be
mentioned, setlocale is the key here. Once the C run-time knows the
final output encoding required you can print from wide strings or
multi-byte stings and both will work.
okay, fourth attempt:
#include <stdio.h>
#include <wchar.h>
#include <stdlib.h>
#include <assert.h>
#include <locale.h>
int main(){
wchar_t vowel;
char utf[8];
/* set output stream to wide-character mode, or halt. */
assert(fwide(stdout, 1) > 0);
assert (setlocale(LC_ALL, "en_US.utf8") != NULL);
wprintf(L"ä \n");
}
This prints a lower-case 'a'. That's better, but still wrong.
Does "en_US.utf8" suppress accents?
fifth attempt:
#include <stdio.h>
#include <wchar.h>
#include <stdlib.h>
#include <assert.h>
#include <locale.h>
int main(){
wchar_t vowel;
char utf[8];
/* set output stream to wide-character mode, or halt. */
assert(fwide(stdout, 1) > 0);
assert (setlocale(LC_ALL, "POSIX") != NULL);
wprintf(L"ä \n");
}
does not change anything, this still prints a lower-case 'a'.
Hmm, whatever 'locale' the darn terminal is using allows ä to show up,
so against the advice of another poster I'll try the empty string with
setlocale().
sixth attempt:
#include <stdio.h>
#include <wchar.h>
#include <stdlib.h>
#include <assert.h>
#include <locale.h>
int main(){
wchar_t vowel;
char utf[8];
/* set output stream to wide-character mode, or halt. */
assert(fwide(stdout, 1) > 0);
assert (setlocale(LC_ALL, "") != NULL);
wprintf(L"ä \n");
}
Changes nothing. it *still* prints a lower-case 'a'.
locale -a on my system returns
C
en_US.utf8
POSIX
the first is the default locale for C programs and restricted to 7-bit
characters according to the setlocale manpage.
the second is what my term programs are set to, and they show most unicode
characters fine.
But none of them work. /usr/share/i18n/SUPPORTED lists 417 more. I decided
I would try the a german locale, since it was explicitly recommended
upthread.
seventh attempt:
sixth attempt:
#include <stdio.h>
#include <wchar.h>
#include <stdlib.h>
#include <assert.h>
#include <locale.h>
int main(){
wchar_t vowel;
char utf[8];
/* set output stream to wide-character mode, or halt. */
assert(fwide(stdout, 1) > 0);
assert (setlocale(LC_ALL, "de_DE.UTF-8") != NULL);
wprintf(L"ä \n");
}
This time the call to setlocale returned NULL so the assert failed.
I suppose that means I need to download the corresponding locale data
before I can do that?
Bear,
still having no luck....
.
- Follow-Ups:
- Re: attempting to print unicode characters.
- From: Ben Bacarisse
- Re: attempting to print unicode characters.
- From: Geoff
- Re: attempting to print unicode characters.
- References:
- attempting to print unicode characters.
- From: Ray
- Re: attempting to print unicode characters.
- From: Ben Bacarisse
- attempting to print unicode characters.
- Prev by Date: Re: Parentheses in function calls
- Next by Date: Re: attempting to print unicode characters.
- Previous by thread: Re: attempting to print unicode characters.
- Next by thread: Re: attempting to print unicode characters.
- Index(es):
Relevant Pages
|