Lead by this page(How to print Unicode character in C++?), I can print Russian "ф".but when try to print "m³"(\u 33a5), I got a "?".
Please anyone can help me.
Console normally does not support displaying unicode characters. Try solution for this question Unicode characters in Windows command line - how?
Try this:
#include <iostream>
#include <fcntl.h>
#include <io.h>
int main()
{
_setmode(_fileno(stdout), _O_U16TEXT);
std::wcout << L"m\u00B3" << std::endl;
return 0;
}
Related
It seems USC2 characters are okey with wchar_t.
However when a codepoint is more than U+FFFF, it just doesn't work.
#include <iostream>
#include <fcntl.h>
#include <io.h>
int main()
{
_setmode( _fileno(stdout), _O_U8TEXT ); // _O_WTEXT has no difference
std::wcout.sync_with_stdio(false);
std::wcout << L"z\u00df\u6c34\U0001F34C\n" << std::endl; // L"zß水🍌"
}
Print as below:
zß水�
How to print SMP characters on console app in Windows?
Is it possible without C++/WinRT?
i m trying to read and write Farsi characters in c++ and i want to show them in CMD
first thing i fix is Font i add Farsi Character to that and now i can write on the screen for example ب (uni : $0628) with this code:
#include <iostream>
#include <io.h>
#include <fcntl.h>
using namespace std;
int main() {
_setmode(_fileno(stdout), _O_U16TEXT);
wcout << L"\u0628 \n";
wcout << L"ب"<<endl;
system("pause");
}
but how i can keep this character ... for Latin characters we can use char or string but how about Farsi character utf8 ?!
and how i can get them ... for Latin characters we use cin>>or gets_s
should i use wchar_t? if yes how?
because with this code it show wrong character ...
wchar_t a='\u0628';
wcout <<a;
and i can't show this character بـ (uni $FE91) even though that exist in my installed font but ب (uni $0628) showed correctly
thanks in advance
The solution is the following line:
wchar_t a=L'\u0628';
The use of L tells the compiler that your type char is a wide char ("large" type, I guess? At least that's how I remember it) and this makes sure the value doesn't get truncated to 8 bits - thus this works as intended.
UPDATE
If you are building/running this as a console application in Windows you need to manage your code pages accordingly. The following code worked for me when using Cyrillic input (Windows code page 1251) when I set the proper code page before wcin and cout calls, basically at the very top of my main():
SetConsoleOutputCP(1251);
SetConsoleCP(1251);
For Farsi I'd expect you should use code page 1256.
Full test code for your reference:
#include <iostream>
#include <Windows.h>
using namespace std;
void main()
{
SetConsoleOutputCP(1256); // to manage console output
SetConsoleCP(1256); // to properly process console input
wchar_t b;
wcin >> b;
wcout << b << endl;
}
I'm relatively new to WinAPI programming in C++. I'm trying to write a program that will obtain the system hostname using GetComputerName(). Ideally, I want the code to be able to work on English and non-English systems. Below is the code that I'm using:
int main()
{
wstring hostname;
wchar_t nbtName[MAX_COMPUTERNAME_LENGTH + 1];
DWORD length = MAX_COMPUTERNAME_LENGTH + 1;
GetComputerName(nbtName, &length);
hostname = nbtName;
wcout << hostname << endl;
return 0;
}
The code works fine on my English Windows 7 system, but the code doesn't seem to display properly on my German Windows 7 system (which uses German characters for the hostname). I thought that wstring and wchar_t could handle these special characters. Here's what's displayed on my German Windows 7 system.
COMPUTER-Í─▄▀
Am I overlooking something stupid? Thanks!
Use _setmode(_fileno(stdout), _O_U16TEXT) to show Unicode in console window:
#include <iostream>
#include <string>
#include <io.h> //for _setmode
#include <fcntl.h> //for _O_U16TEXT
int main()
{
_setmode(_fileno(stdout), _O_U16TEXT);
std::wcout << L"ελληνικά\n";
return 0;
}
Or use MessageBoxW(0, hostname.c_str(), 0, 0) or OutputDebugStringW to see Unicode text displayed correctly:
this function has two versions GetComputerNameW for unicode and GetComputerNameA for ANSI. if UNICODE is defined in your environment the the first one will be called. So either make sure UNICODE is defined or try to call the GetComputerNameW directly.
Windows Console output was the culprit. The Unicode characters display correctly in other non-console output. Thanks everyone!
I'm trying to print the Airplane symbol using Unicode in my CodeBlocks. I found out that the code of Airplane is \u2708. So I tried the following code:
#include <iostream>
using namespace std;
int main() {
wchar_t a = '\u2708';
cout << a;
return 0;
}
It outputs 40072 when I replace wchar_t with char
char a = '\u2708';
I get this symbol: ł
Im really stuck, thanks for any help.
If you are in Linux and dont use codepage conversion from unicode to console try this:
std::locale::global(std::locale(""));
wchar_t plane = L'\u2708';
std::wcout << plane << std::endl;
In windows is a bit more complicated, you need a compatible unicode font on the default console and the correct codepage.
Is it possible to print subscripts/superscripts ?
for example like that : x²
what are functions allow to do that ?
This depends entirely on the environment you're running in. For a GUI system (Windows, Mac, Qt, etc.) you would need to consult the API documentation. For a text mode system, the best you could do is use specific characters in your current encoding. For instance, unicode has certain code points that are super- or sub-scripts of other characters.
If you're using a GUI, you can change the size and orientation of the font.
There are also superscript and subscript characters available in Unicode that could be used.
You can print the appropriate Unicode symbol, to cout or wcout depending on locale:
#include <iostream>
int main()
{
std::cout << "x\u00b2" << std::endl;
}
or
#include <iostream>
#include <locale>
int main()
{
std::locale::global(std::locale("de_DE.UTF8"));
std::wcout << L"x\u00b2" << std::endl;
}