3

Greetings,

I'm developing a project in C++ where I want to use characters like á é õ and ┌ ─ ┐ │ to draw a couple of nice frames. My doubt resides in what I should change in my code/project settings since, without any kind of modifications, the console just prints pseudo-random characters.

I know that the above characters are defined in the character set Code page 437 aka Extended ASCII, but what I should do know?

After some research, I included the instruction setlocale(LC_CTYPE, ""); and now I can print accented characters (à é õ) but can't print the borders.

Also, should I use char and string or wchar and wstring to use these characters?

Thanks

James McNellis
  • 348,265
  • 75
  • 913
  • 977
Renato Rodrigues
  • 1,038
  • 1
  • 18
  • 35
  • Your application may be using a specific local. But what local/character encoding is the terminal using to display the characters? – Martin York Oct 23 '10 at 22:50

3 Answers3

2

I think the best way to do it would be to use wchar and wstring for the characters - they are meant for locale-independant string operations and are defined as UTF-16 in Windows and as UTF-32 in Linux.

Note that you need to use the proper functions, for example wprintf instead of printf... If you're using iostream, I think that should work out-of-the-box with wstrings.

EDIT: Note that it is not required for wchar_t to be unicode (in practice, it often is). If wchar_t (and thus, wstring) is unicode, then the C99 standard (and therefore most likely the C++ standard) states that __STDC_ISO_10646__ is to be defined.

In other words, if __STDC_ISO_10646__ is defined, then the wchar_t is unicode -- as for the exact type (UTF-16 or UTF-32), you can use a sizeof(wchar_t).

Tim Čas
  • 10,501
  • 3
  • 26
  • 32
  • In C++ (which is based on C89, not C99), what encoding `wchar_t` and `std::wstring` have is an implementation detail, although what you state for Windows/Linux is the common case. For stream IO, use `std::wcout`, `std::wcin` etc. – sbi Oct 23 '10 at 22:49
  • I removed the `setlocale(LC_CTYPE, "");` since wchar and wstring are locale-independant. But now, the compiler outputs the error `converting to execution character set: Illegal byte sequence` with operations like `std::wstring maintenance = L"Manutenção";` What's happening? – Renato Rodrigues Oct 25 '10 at 13:16
  • @sbi: Same goes for C99, it is implementation-dependant - but, as I said `__STDC_ISO_10646__` is defined if whatever wchar_t is, is some form of Unicode. – Tim Čas Oct 25 '10 at 17:11
  • @renatorodrigues: Hm, I'm not sure what could be causing that, to be quite honest... **EDIT:** Maybe LC_CTYPE also refers to the locale of string literals? As in, the current locale you're editing with. That's more or less a wild guess though. – Tim Čas Oct 25 '10 at 17:12
  • IIUC, `__STDC_ISO_10646__` is a _C99_ macro. It _might_ be defined by a C++ compiler, but doesn't have to, because C++ doesn't officially know about C99. – sbi Oct 25 '10 at 17:21
0

Try using wchar_t

Community
  • 1
  • 1
Jacob
  • 34,255
  • 14
  • 110
  • 165
0

To see which Unicode characters to use look up http://unicode.org/charts/ and search for "Box Drawing". That set includes the characters that are in CP 437, but also many others. Next question is whether your device can show some or all of these.

Asmus
  • 1