3

I wanted to know how to display special characters with printf().
I'm doing a string conversion program from Text to Code128 (barcode encoding).
For this type of encoding I need to display characters such as Î, Ç, È, Ì.

Example:
string to convert: EPE196000100000002260500004N
expected result: ÌEPEÇ3\ *R 6\ R $ÈNZÎ
printf result typed: ╠EPEÇ3\ *R 6\ R $ÇNZ╬
printf result image: [enter image description here]

EDIT: I only can use C in this program no C++ at all. All the awnsers I've find so far are in C++ not C so I'm asking how to do it with C ^^

Cœur
  • 37,241
  • 25
  • 195
  • 267
MarceauC
  • 51
  • 1
  • 1
  • 5
  • 1
    Just by printing them. But do note that the encoding of your console or other output device will affect it, as well as your code's codepage. Better use hex codes if you need specific codes. – Sami Kuhmonen Oct 09 '15 at 10:15
  • ok gonna try this, thx a lot ;) – MarceauC Oct 09 '15 at 10:26
  • I took Ascii/Hexa code from http://www.ascii-code.com/ I tied to display ÌÈÎÇ with printf("%c%c%c%c\n", '\xCC', '\xC8', '\xCE','\xC7'); result: ╠╚╬ Ã beautiful but not I expected ... :/ Do you have any idea on how to fix it ? ^^' – MarceauC Oct 09 '15 at 10:37
  • Possible duplicate of [Output Unicode to Console Using C++](http://stackoverflow.com/questions/2849010/output-unicode-to-console-using-c) – phuclv Oct 09 '15 at 10:54
  • 1
    [How to print Unicode character in C++?](http://stackoverflow.com/q/12015571/995714), [Printing UTF-8 strings with printf - wide vs. multibyte string literals](http://stackoverflow.com/q/15528359/995714), [How to output unicode characters in C/C++](http://stackoverflow.com/q/17641718/995714), and a just-asked question: [Is it possible to cout an EM DASH on Linux and Windows? (C++)](http://stackoverflow.com/q/33029906/995714) – phuclv Oct 09 '15 at 10:56
  • 1
    If you print `'\xCC', '\xC8', '\xCE','\xC7'` and see 4 different characters it means that your console is not in UTF-8 encoding. Probably your console does not have required characters (it be can checked by printing all printable chars 32-255 in a loop). You may think how to convert your chars to UTF-8 and then use UTF-8 console. – Orest Hera Oct 09 '15 at 10:58
  • It looks that in your console you need codes '\x8c', 'x8d' http://asciiset.com/ – Orest Hera Oct 09 '15 at 11:02
  • all this awnsers help a lot to better understand the problem but do not solve it as I can't use C++ in this program... :/ I only can use C ^^' I've research a lot before asking and only find C++ awnsers, and as it is the final step of this program it would be very annoying to not have correct results... – MarceauC Oct 09 '15 at 13:28

2 Answers2

2

I've find it,

#include <locale.h>
int main()
{
setlocale(LC_ALL,"");
 printf("%c%c%c%c\n", 'Î', 'Ç', ' È','Ì');
}

Thank you all for your awnsers it helps me a lot!!! :)

MarceauC
  • 51
  • 1
  • 1
  • 5
  • Note that this code is not portable, since such symbols ('Ç') may be stored in your source file as a single byte or as few Unicode bytes. That depends on your text editor. If the text editor is Non-Unicode the binary codes of those symbols depend on editor encoding. – Orest Hera Oct 09 '15 at 14:49
1

If your console is in UTF-8 it is possible just to print UTF-8 hex representation for your symbols. See similar answer for C++ Special Characters on Console

The following line prints heart:

printf("%c%c%c\n", '\xE2', '\x99', '\xA5');

However, since you print '\xCC', '\xC8', '\xCE','\xC7' and you have 4 different symbols it means that the console encoding is some kind of ASCII extension. Probably you have such encoding http://asciiset.com/. In that case you need characters '\x8c', 'x8d'. Unfortunately there are no capital version of those symbols in that encoding. So, you need some other encoding for your console, for example Latin-1, ISO/IEC 8859-1.


For Windows console:

UINT oldcp = GetConsoleOutputCP(); // save current console encoding

SetConsoleOutputCP(1252);
// print in cp1252 (Latin 1) encoding: each byte => one symbol
printf("%c%c%c%c\n", '\xCC', '\xC8', '\xCE','\xC7');

SetConsoleOutputCP(CP_UTF8);
// 3 hex bytes in UTF-8 => one 'heart' symbol
printf("%c%c%c\n", '\xE2', '\x99', '\xA5');

SetConsoleOutputCP(oldcp);

The console font should support Unicode (for example 'Lucida Console'). It can be changed manually in the console properties, since the default font may be 'Raster Fonts'.

Community
  • 1
  • 1
Orest Hera
  • 6,706
  • 2
  • 21
  • 35
  • Thank's a lot!! I gonna investigate in that way :) – MarceauC Oct 09 '15 at 12:57
  • I tried this to change console encoding #include int main() { SetConsoleOutputCP(CP_UTF8); printf("%c%c%c%c\n", '\u00CE', 'Ç', ' È','Ì'); return 0; } but stills showing things I don't want to see ... ^^' http://img110.xooimage.com/views/a/e/9/capture1-4d0699b.png/ – MarceauC Oct 09 '15 at 13:21
  • @MarceauC Now it is more clear that you work with Windows console. I updated the answer with Windows example. – Orest Hera Oct 09 '15 at 14:20