Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C: Display special characters with printf()

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 ^^

like image 602
MarceauC Avatar asked Dec 06 '25 03:12

MarceauC


2 Answers

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!!! :)

like image 193
MarceauC Avatar answered Dec 07 '25 18:12

MarceauC


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'.

like image 43
Orest Hera Avatar answered Dec 07 '25 20:12

Orest Hera



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!