In one of my application I'm developing in c++, I have to display "✓" mark. For that I need to first insert the same in a std::string or in a char. But when I do that, I'm getting a "?" mark as output. I'm using VS2010 to code. Please suggest how to solve the same. Thanks in advance.
Asked
Active
Viewed 4,328 times
1
-
1Try looking at http://stackoverflow.com/questions/12015571/how-to-print-unicode-character-in-c and see if it answers your question. – Sean Duggan Mar 20 '14 at 12:33
-
`std::string` isn't meant for processing unicode characters. – Bartek Banachewicz Mar 20 '14 at 12:35
-
Hi Sean Duggan, in the page you mentioned, its a char, but I need the same in std::string. Any help would be greatly appreciated. Thank you. – user3164377 Mar 20 '14 at 12:40
-
Do you want console output or is it some label in a GUI application? If the former, I suggest using `WriteConsoleW` directly, or using any of the solutions presented [in this question](http://stackoverflow.com/questions/2492077/output-unicode-strings-in-windows-console-app). Also remember to set your console font to something that has the character you're trying to print. – rubenvb Mar 20 '14 at 12:40
-
Hi rubenvb, its for a label in a GUI. – user3164377 Mar 20 '14 at 12:44
-
1@user3164377 then use the GUI's unicode-aware string class, or in the worst case, a `std::wstring` (on Windows, `std::string` will do fine on other OSes). In Visual Studio, you should be able to copy-paste the glyph from this web page into a string or (wide) character literal. – rubenvb Mar 20 '14 at 12:49
-
@rubenvb. Can you please eloborate which webpage you are talking about? and I'm uaing Win7 for this application. – user3164377 Mar 20 '14 at 12:54
-
@user3164377 I mean writing `std::wstring checkmark = L"✓";` in your editor (and saving the source file as UTF-8). – rubenvb Mar 20 '14 at 13:56
-
@rubenvb. Hi. I used std::wstring s = L"✓"; and std::string st(s.begin(), s.end()); to convert wstring to std::string. It did not work either. Please suggest some solution. I know I came close to the point, but lil push needed. Thank you. – user3164377 Mar 21 '14 at 05:18
2 Answers
2
There seems to be some basic misunderstanding.
The checkmark character is Unicode 0x2713. You cannot store it as a single character in std::string. The maximum value for a char is 0xff (255). It won't fit.
If you are developing a GUI using C++ for Windows then I would guess MFC. However if you are using std::string then perhaps that's not so. Some choices:
- For MFC, you can rebuild your application in UNICODE mode. Then chars are short (16 bits) and your checkmark will fit fine.
- You could use std::wstring instead of string. That means changes to existing code.
- You could use UTF-8, which replaces the character by a multi-byte sequence. Not recommended in Windows, even if you think you know what you're doing. Very unfriendly.
In any case, if you are using GUI and dialog boxes you will have to make sure they are Unicode dialog boxes or nothing will work.
With a few more details, we could give more specific advice.

david.pfx
- 10,520
- 3
- 30
- 63
-
Hi, Thanks for your help. My GUI supports unicodes. Problem is I'm using VS2010 in Windows to develop this code. I'm not able to convert the unicode to std::string. I tried using std::wstring s = L"✓"; and std::string st(s.begin(), s.end()); to convert wstring to std::string. It did not work either. Please suggest some solution. I know I came close to the point, but lil push needed. Thank you – user3164377 Mar 21 '14 at 05:47
-
As per my answer you cannot ever convert a 16-bit Unicode character such as 0x2713 to an 8-bit char. It just won't fit. Explain what you're trying to achieve or show code and there may be a solution. – david.pfx Mar 21 '14 at 07:26
0
you can insert check mark in your console using C++ using the following code
cout << " (\xfb) "<<endl;
Output:
(√)