2

I am trying to insert the Unicode character U+2022 (bullet ) in my C++ application.

I can't figure out how to convert that U+2022 to a char/string for use in std::string constructor...

char bullet = char(0x2022);
mPassword.SetText( std::string(mText.length(), bullet) );

This one doesn't work. Hope you can help !!

Thanks
opatut

javac
  • 2,431
  • 4
  • 17
  • 26
opatut
  • 6,708
  • 5
  • 32
  • 37
  • 1
    possible duplicate of [How can I embed unicode string constants in a source file?](http://stackoverflow.com/questions/442735/how-can-i-embed-unicode-string-constants-in-a-source-file) – Kirill V. Lyadvinsky Sep 15 '10 at 17:10

2 Answers2

4

Unicode character has type wchar_t(see §2.13.4 of the C++ Standard). You could use it as follows:

wchar_t bullet = L'\x2022';

In string it will look like:

std::wstring str_w_bullet( L"some text with \x2022" );
Kirill V. Lyadvinsky
  • 97,037
  • 24
  • 136
  • 212
  • It should be noted that depending on your locale in Linux, std:string is technically treated as a series of UTF-8 code units, so it deals with unicode correctly. However, I have no idea how you'd set any of them individually if you wanted anything other than ASCII and wanted to set it in the code rather than reading in something that's already unicode (you'd probably have to figure out what each code unit was rather than setting a whole code point at once). However, on Windows, I think that you do indeed have to use std::wstring. – Jonathan M Davis Sep 15 '10 at 17:29
  • I still have a problem. I simply do not get the bullet point on my output. std::wcout outputs quotes (") – opatut Sep 15 '10 at 17:53
  • 1
    The problem is in the console. Check [this](http://stackoverflow.com/questions/2492077/output-unicode-strings-in-windows-console-app) question if you use Windows. – Kirill V. Lyadvinsky Sep 15 '10 at 18:03
1

use std::wstring which is that same as std::string but specialized on wchar_t

Steve Townsend
  • 53,498
  • 9
  • 91
  • 140