How can I print a string like this: €áa¢cée£ on the console/screen? I tried this:
#include <iostream> #include <string> using namespace std; wstring wStr = L"€áa¢cée£"; int main (void) { wcout << wStr << " : " << wStr.length() << endl; return 0; } which is not working. Even confusing, if I remove € from the string, the print out comes like this: ?a?c?e? : 7 but with € in the string, nothing gets printed after the € character.
If I write the same code in python:
#!/usr/bin/env python # -*- coding: utf-8 -*- wStr = u"€áa¢cée£" print u"%s" % wStr it prints out the string correctly on the very same console. What am I missing in c++ (well, I'm just a noob)? Cheers!!
Update 1: based on n.m.'s suggestion
#include <iostream> #include <string> using namespace std; string wStr = "€áa¢cée£"; char *pStr = 0; int main (void) { cout << wStr << " : " << wStr.length() << endl; pStr = &wStr[0]; for (unsigned int i = 0; i < wStr.length(); i++) { cout << "char "<< i+1 << " # " << *pStr << " => " << pStr << endl; pStr++; } return 0; } First of all, it reports 14 as the length of the string: €áa¢cée£ : 14 Is it because it's counting 2 byte per character?
And all I get this:
char 1 # ? => €áa¢cée£ char 2 # ? => ??áa¢cée£ char 3 # ? => ?áa¢cée£ char 4 # ? => áa¢cée£ char 5 # ? => ?a¢cée£ char 6 # a => a¢cée£ char 7 # ? => ¢cée£ char 8 # ? => ?cée£ char 9 # c => cée£ char 10 # ? => ée£ char 11 # ? => ?e£ char 12 # e => e£ char 13 # ? => £ char 14 # ? => ? as the last cout output. So, actual problem still remains, I believe. Cheers!!
Update 2: based on n.m.'s second suggestion
#include <iostream> #include <string> using namespace std; wchar_t wStr[] = L"€áa¢cée£"; int iStr = sizeof(wStr) / sizeof(wStr[0]); // length of the string wchar_t *pStr = 0; int main (void) { setlocale (LC_ALL,""); wcout << wStr << " : " << iStr << endl; pStr = &wStr[0]; for (int i = 0; i < iStr; i++) { wcout << *pStr << " => " << static_cast<void*>(pStr) << " => " << pStr << endl; pStr++; } return 0; } And this is what I get as my result:
€áa¢cée£ : 9 € => 0x1000010e8 => €áa¢cée£ á => 0x1000010ec => áa¢cée£ a => 0x1000010f0 => a¢cée£ ¢ => 0x1000010f4 => ¢cée£ c => 0x1000010f8 => cée£ é => 0x1000010fc => ée£ e => 0x100001100 => e£ £ => 0x100001104 => £ => 0x100001108 => Why there it's reported as 9 than 8? Or this is what I should expect? Cheers!!
# -*- coding: utf-8 -*-in python? How do I know or set that in c++? I just use vim to write the script. Cheers!!uniTest.cpp: UTF-8 Unicode c program text. I'd be surprised if it wasn't. The vim is being used in the very same way for every thing, whilst, e.g. python works but c++ not. Anything else do you thing still missing? cheers!