ascii /unicode

how do i insert ascii characters to the code :) .

1
2
3
cout << "***************************************"<<endl;
cout << "  \263a        GOOD BYE!!!\a              "<<endl;
cout << "***************************************"<<endl;
U+263A (WHITE SMILING FACE) is not an ASCII character.

 
std::wcout << (wchar_t)0x263A;
Your console may or not render this correctly.
Last edited on
97869786 i got this instead of that character after using this :(
9786 is 0x263A in decimal.
You might try:

 
    std::cout << "\u263A" << '\n';

dutch i used it and i got a "?"output
that is likely just an output problem. For example you get gibberish if you try to open a unicode text file in an ascii text editor, and you get ? or other placeholder symbols when writing to a console/terminal that does not support it, etc.
Use UTF-8.


The UTF-8 encoding of Unicode U+263A is "\xE2\x98\xBA". You can literally google any U+ code point and get a gazillion sites that want to tell you what it is and what it looks like in any UTF encoding you want.

Just as easy, though, is to use an editor that will save your source file as UTF-8, and insert the character directly. "☺" Check your compiler documentation to make sure it takes UTF-8 input, and compile like normal.


The next important thing to do is make sure your output device (the terminal) accepts UTF-8. On Windows, you can type "chcp 65001" to make sure your console is in UTF-8 mode. Or, you can use
https://www.google.com/search?btnI=1&q=msdn+SetConsoleOutputCP
in your code to fix that, if you are willing to #include <windows.h> .

Another option would be to create a batch file that sets things up for your program and runs it.
1
2
3
@echo off
chcp 65001
my_program.exe %*

You will have to be a little creative on naming, though, since EXE files take precedence over same-name BAT files. (Stupid, yes, but alas, DOS did it that way...)


Good luck!
for little at home / school projects, there isnt any reason to tie yourself in a knot trying to avoid system. system("chcp 65001"); in your code is fine. If its code that might be at risk from someone putting a nefarious chcp.bat in your folder or path, you can upgrade your paranoia level at that time.

I need to write that command down. I keep forgetting it, glad you guys knew it.
Last edited on
thank you everyone it was very helpful :)
Topic archived. No new replies allowed.