Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi! I've been learning unicode from this tutorial:
What are TCHAR, WCHAR, LPSTR, LPWSTR, LPCTSTR (etc.)?[^]
and stumbled upon some code problems which I have no idea how to fix.

My problems are :
1)
C++
wchar_t ch2[] = L"black☺";
wcout << ch2;

nothing happens. If I remove the smiley face then wcout types it. Isn't wcout supposed to be able to type unicode? What's up with that?

2)
C++
wchar_t ch2[] = L"black☺";
wchar_t *cht = new wchar_t;
int size = wcslen(ch2);
wcscpy_s(cht, size+1, ch2);
wcout << cht << endl;

If I compile this code then only the "black" part is printed on the screen, then smiley face is not and the "endl" code doesn't even work (so I can't end line unless I remove the smiley face);

3)
C++
wchar_t *te = new wchar_t;
wcin >> te;
wcout << te << endl;


The output is always nothing no matter what I type...

I just don't get it... Either I'm doing something completely wrong or c++ just made typing unicode too hard for my understanding. Please help me figure out my mistakes.
Posted
Comments
KarstenK 24-Jun-14 3:47am    
1. the smiley isnt a valid unicode char for output on console.

2. + 3. you a have a memory bug. use:

wchar_t *cht = new wchar_t[6];//alloc with size of 6
Supraspinatus 24-Jun-14 4:13am    
Thanks a lot for the answer! But I still got some problems regarding them so I'll ask them.
1) It is possible to output the smiley on console, the code for it is:
char c = 1;
cout << c << endl;
So it must be possible to make a smiley with unicode and wchar_t too
2)Did not fix the problem. The output is the same and still I can't type a ☺ with wchar_t. And it makes the same problem for any other language letters too.
3) Still doesn't work
KarstenK 24-Jun-14 4:22am    
1. it is an error behaviour.

sorry, I really dont know. (that why a comment and no solution)

 
Share this answer
 
Comments
Supraspinatus 24-Jun-14 9:06am    
Thanks for your solution CPallini but unfortunately it isn't working. The source code doesn't bring the results I want and when I understood the step by step guide and wrote my version of the code (which is basically the same) I got something completely random.
Here's my code:

UINT oldcp = GetConsoleOutputCP();
SetConsoleOutputCP(CP_UTF8);

wchar_t output[] = L"почему у меня не хера не работает";
int buffSize = WideCharToMultiByte(CP_UTF8, 0, output, -1, NULL, 0, NULL, NULL);
char *buffer = new char[buffSize];
WideCharToMultiByte(CP_UTF8, 0, output, -1, buffer, buffSize, NULL, NULL);
wprintf(L"%S", buffer);
delete []buffer;

SetConsoleOutputCP(oldcp);


Here's the output:
????NЗ?╡??N? N? ???╡??N? ???╡ NЕ?╡NИ?░ ???╡ NИ?░?▒??NВ?░?╡NВ
For one, don't rely on the smiley-character in your string to have the correct intended character code. Try to use the unicode escape \u263a instead. In other words, try
C++
wchar_t ch2[] = L"black\u263a";

The second mistake in your program is in the line
C++
wchar_t *cht = new wchar_t; // wrong!

You are allocating room for exactly one wide-character. What you probably intended is something like:
C++
int size = wcslen(ch2);
wchar_t *cht = new wchar_t[size+1];
wcscpy_s(cht, size+1, ch2);
...
 
Share this answer
 
Comments
Supraspinatus 24-Jun-14 17:32pm    
Thanks for your answer, but still even the
wchar_t ch2[] = L"black\u263a";
is bringing the same result... Thanks for correcting my code btw I'm still fairly new to c++.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900