Posts tagged unicode
How to include special characters in string?
3![]()
In C++ programs, sometimes you’ve to add special chars to your strings. For instance a copyright message – “Copyright © WeSeeTips.com”. If your editor like Visual Studio support unicode, you can directly paste it to the source. But not all editors are unicode aware. So what’s the safest method?
![]()
You can use C++ escape sequence – \x. The \x followed by hexadecimal value of char will insert that char to the string. For instance the hex of © is oxA9. See how it can be added to a string.
// 0xA9 is the hex for copyright symbol.
const TCHAR* pCopyright = _T("Copyright \xA9 WeSeeTips.com");
AfxMessageBox( pCopyright );
![]()
Its worth reading about trigraphs chars at this moment.
http://weseetips.com/2008/03/25/trigraph-char-sequences-in-c-c/
![]()
Targeted Audience – Beginners.
How to convert Unicode to Ansi and vice versa – More Easily?
0![]()
For converting between Unicode and Ansi and vice versa, usually we use functions such as MultiByteToWideChar() and WideCharToMultiByte(). But they are bit hard to use. Is there any other easy way to convert Unicode to Ansi and vice versa? Yes! you can use ATL conversion macros.
![]()
In ATL there are some helper macros defined such as W2A() which is used to convert widechar to ansi and A2W which is used to convert ansi to WideChar. Before using this macros you should initialize the conversion by calling USES_CONVERSION. See the code snippet below.
#include "atlconv.h" ... // Ansi string. char* pstrString = "Hello There!"; // Unicode string. wchar_t* pUnicodeString = 0; // Use this macro for Initializing the conversion. // Actually, some member variable initialization deep inside. USES_CONVERSION; // Convert Ansi to Unicode. pUnicodeString = A2W( pstrString); // Convert Unicode to Ansi. pstrString = W2A( pUnicodeString);
![]()
Targeted Audience – Beginners.