The Wayback Machine - https://web.archive.org/web/20160318200941/http://www.codeguru.com/cpp/cpp/string/alts/article.php/c2799/CUnicodeString-Class.htm

CUnicodeString Class

CUnicodeString is a replacement for CString. If you are one of these poor guys that has to write applications that use for example NetXxx functions and don't want to create a Unicode project, or use ANSI only functions from a unicode project or what ever, you always had to deal with MultiByteToWideChar and WideCharToMultibyte. Maybe you even have to play with native functions witch uses counted unicode strings (UNICODE_STRING) or LSA (LSAUNICODE_STRING). If you were in such situations and hated the fact that the MFC CString doesn't help you with these conversions, than you would love this little class...

CUnicodeString has all the functions that CString provides, so you can use it allmost everywhere where you use CString normaly. This class works internally only with Unicode strings. Every string assignment is converted to and stored as Unicode.

And the best of it: It's easy to use... :)

Example:


CUnicodeString cs;
WCHAR wc = L"My Wide String";
cs = wc;
CHAR ch = " needs a few ANSI chars to append";
cs += wc;
cs += " ";
LPSTR pstrMyText = cs;
LPWSTR pstrMyUnicodeText = cs;
cs.Empty();
cs = "MyUserName";
NetUserGetInfo(cs,....);
...
This class was written for a upcomming C++ library called SecLIB witch covers the whole NT-Security. This is one of the reasons why it internally works in Unicode. BTW: Using System API's in Unicode is allways faster in NT, because NT internally works also with Unicode only... :)

I hope you like it! :)

Alexander Keck
MCSE, MCDBA
(MCSD in progress....)

Downloads

Download source - 6 Kb


Comments

  • Arabic text processing

    Posted by salton5 on 05/11/2006 04:49pm

    dear I will appreciate your help very much if you have a class or any comment on processing arabic text. ( for my project) regards Salton

    Reply
  • CUnicodeString::operator += (LPCSTR pstrString)

    Posted by allos on 02/24/2006 05:32am

    Hi 
    I found this class very usefull but also I found a bug in 
    void CUnicodeString::operator += (LPCSTR pstrString)
    {
    	ASSERT(IsValidString(pstrString));
    	USHORT usNewLength = m_Length + strlen(pstrString);
    	LPWSTR pstrNewString = (LPWSTR)malloc((usNewLength + 1) * sizeof(WCHAR));
    	wcscpy(pstrNewString, m_Buffer);
    	MultiByteToWideChar(CP_ACP, 0, pstrString, strlen(pstrString), &pstrNewString;[m_Length], usNewLength - m_Length);
    	free(m_Buffer);
    	m_Buffer = pstrNewString;
    	m_Length = usNewLength;
    	m_MaximumLength = m_Length;
    }
    
    I had to replace this code with
    
    void CUnicodeString::operator += (LPCSTR pstrString)
    {
    	ASSERT(IsValidString(pstrString));
    	USHORT usNewLength = m_Length + strlen(pstrString);
    	LPWSTR pstrNewString = (LPWSTR)malloc((usNewLength + 1) * sizeof(WCHAR));
    	wcscpy(pstrNewString, m_Buffer);
    	MultiByteToWideChar(CP_ACP, 0, pstrString, strlen(pstrString), &pstrNewString;[m_Length], usNewLength - m_Length);
            pstrNewString[m_Length]=L'\0';
    	free(m_Buffer);
    	m_Buffer = pstrNewString;
    	m_Length = usNewLength;
    	m_MaximumLength = m_Length;
    }
    
    otherwise I got an error using the strings in other operations.

    Reply
  • How to solve this

    Posted by Kumar_poruki on 03/24/2005 10:47pm

    if a sequence of input bytes doesnot form a valid character then the function must skip the invalid character or replace with default multibyte character of similar type to continue the conversion from GB to UTF8 ?

    Reply
  • UCS 2

    Posted by Legacy on 02/04/2003 12:00am

    Originally posted by: Wael

    Hello ,
    how to convert to UCS 2 and FROM UCS 2

    wael

    Reply
  • Is this 32-bit UNICODE or 16-bit UNICODE ?

    Posted by Legacy on 01/08/2003 12:00am

    Originally posted by: John

    UNICODE is a character set that can be expressed in
    many different codesets. Which one do you support ?

    I'm guessing one of the following, and the intel
    byte-ordering version of it:

    UCS-2 ? (From the old Unicode 3.0)

    UTF-16 ? (A variable width coding)

    Reply
  • Problem with WideCharToMultiByte() function

    Posted by Legacy on 09/06/2002 12:00am

    Originally posted by: Virginie

    Hi !
    
    I saw your code and I think you can help me !

    I am working with Win2000Pro, Microsoft Visual C++ 6 and I'm using the _UNICODE preprocessor directive.

    In my application I have to write an ANSI text into a file, so I'm using the WideCharToMultiByte() function.

    My problem is that, sometimes (really sometimes) come an Unhandled exception by the conversion !
    However the conversion is done and I can write the converted text into my file.
    I implemented a try{} catch(...){} block so the application can go ahead.

    Have you ever hear something like this ? I hope you can help me.

    Thank you for reading this...

    Here is my code :

    // ---------vvvvvvvvvvvvvvvvv----------------------
    void CPart::WriteStringInFile(CMemFile& memFileTRF)
    {
    ULONG longeur;
    BOOL bIsTextUnicode;
    LPSTR lpStrWork = NULL;

    int nWCharSource = memFileTRF.GetLength();
    BYTE* strTmp = memFileTRF.Detach();

    lpStrWork = (LPSTR)malloc((nWCharSource + 2) * sizeof(WCHAR));

    CString strText;
    CMainFrame* pMainFrame=(CMainFrame*)(::AfxGetMainWnd());

    CFile fileTRF;
    CFileException e;

    if (fileTRF.Open(m_strCpdFileName, CFile::modeCreate | CFile::modeWrite, &e;))
    {
    bIsTextUnicode = IsTextUnicode(strTmp,nWCharSource,NULL);

    if (bIsTextUnicode)
    {
    try
    {
    longeur = WideCharToMultiByte(CP_ACP, 0,
    (LPCWSTR)strTmp, nWCharSource, // source
    lpStrWork, nWCharSource, // destination
    NULL, NULL);

    fileTRF.Write(lpStrWork,nWCharSource/2);
    }
    catch (...)
    {
    strText = _T("Probl�me lors de la g�n�ration du fichier TRF");
    fileTRF.Write(lpStrWork,nWCharSource/2);
    }
    }
    else // Text is ANSI
    {
    strText = _T("Text is ANSI and DON'T need a conversion....");
    fileTRF.Write(strTmp,nWCharSource);
    }
    pMainFrame->Trace(0,strText);

    fileTRF.Close();
    }

    if (lpStrWork)
    free(lpStrWork);

    delete strTmp;
    }

    Reply
  • small bug

    Posted by Legacy on 01/01/2002 12:00am

    Originally posted by: Tim Monks

    Hi - just a small thing.  Your args to _vsnwprintf need to give character count, not number of bytes.
    
    

    Use:
    nBuf = _vsnwprintf(szBuffer, sizeof(szBuffer)/sizeof(szBuffer[0]), pstrFormat, args);

    Rather than:
    nBuf = _vsnwprintf(szBuffer, sizeof(szBuffer), pstrFormat, args);

    Tim

    Reply
  • CString already works w/ Unicode...

    Posted by Legacy on 08/07/2001 12:00am

    Originally posted by: sean

    CString already works w/ Unicode strings dOOfey.

    Reply
  • MS Provides equivalent in CHString

    Posted by Legacy on 02/15/2001 12:00am

    Originally posted by: Shane Triem

    Microsoft provides the CHString as part of the Platform SDK: Windows Management Instrumentation.

    In MSDN, refer to "CHString Class Reference"

    I'm sure the class provided Alexander Keck is fine, but I try to stick with the Platform SDK whenever possible. I once posted to CodeGuru how to extract CString from MFC to be used in ATL projects without the MFC DLLs. Now that CHString is available, this is no longer necessary.

    Note: the CHString class is in the library Framedyn.lib. This DLL has a footprint of 164 KB.

    *************************************


    The CHString class is modeled on the Microsoft MFC CString class; the primary difference between CString and CHString is that CHString lacks some of the Unicode support of CString.

    Internally, CHString instances (strings) store information as wide character strings; that is, 2 bytes per character. The default value of a CHString string is the starting address of its first character. CHString strings end with a 2-byte null.

    Reply
  • Misc Errors with += operator

    Posted by Legacy on 11/16/2000 12:00am

    Originally posted by: Phillip Trelford

    Liked the class, handy for manipulating those passed COM BSTRs, but found some errors when using the += operator.
    e.g. Appending to uninitialised string, and the appending of a LPCTSTR.
    You might find my changes useful.

    void CUnicodeString::operator += (const CUnicodeString& usString)
    {
    size_t nLen=wcslen(usString);
    if( nLen==0 ) return;

    m_Length += nLen;
    LPWSTR pstrNewString = (LPWSTR)malloc((m_Length + 1) * sizeof(WCHAR));
    if( m_Buffer )
    {
    wcscpy(pstrNewString, m_Buffer);
    wcscat(pstrNewString, usString);
    free(m_Buffer);
    }
    else
    {
    wcscpy(pstrNewString, usString);
    }
    m_Buffer = pstrNewString;
    m_MaximumLength = m_Length;
    }

    void CUnicodeString::operator += (LPCWSTR pstrString)
    {
    ASSERT(IsValidString(pstrString));
    if( pstrString==NULL ) return;
    size_t nLen=wcslen(pstrString);
    if( nLen==0 ) return;

    m_Length += nLen;
    LPWSTR pstrNewString = (LPWSTR)malloc((m_Length + 1) * sizeof(WCHAR));
    if( m_Buffer )
    {
    wcscpy(pstrNewString, m_Buffer);
    wcscat(pstrNewString, pstrString);
    free(m_Buffer);
    }
    else
    {
    wcscpy(pstrNewString, pstrString);
    }

    m_Buffer = pstrNewString;
    m_MaximumLength = m_Length;
    }

    void CUnicodeString::operator += (LPCSTR pstrString)
    {
    ASSERT(IsValidString(pstrString));
    if( pstrString==NULL ) return;
    size_t nLen=strlen(pstrString);
    if( nLen==0 ) return;

    USHORT usNewLength = m_Length + nLen;
    LPWSTR pstrNewString = (LPWSTR)malloc((usNewLength + 1) * sizeof(WCHAR));
    if( m_Buffer )
    {
    wcscpy(pstrNewString, m_Buffer);
    }
    MultiByteToWideChar(CP_ACP, 0, pstrString, nLen, &pstrNewString;[m_Length], nLen);
    pstrNewString[usNewLength]=L'\0';
    if( m_Buffer )
    {
    free(m_Buffer);
    }
    m_Buffer = pstrNewString;
    m_Length = usNewLength;
    m_MaximumLength = m_Length;
    }

    void CUnicodeString::operator += (WCHAR wch)
    {
    m_Length++;
    LPWSTR pstrNewString = (LPWSTR)malloc((m_Length + 1) * sizeof(WCHAR));
    if( m_Buffer )
    {
    wcscpy(pstrNewString, m_Buffer);
    }
    pstrNewString[m_Length - 1] = wch;
    pstrNewString[m_Length] = L'\0';
    if( m_Buffer )
    {
    free(m_Buffer);
    }
    m_Buffer = pstrNewString;
    m_MaximumLength = m_Length;
    }

    void CUnicodeString::operator += (CHAR ch)
    {
    USHORT usNewLength = m_Length + 1;
    LPWSTR pstrNewString = (LPWSTR)malloc((usNewLength + 1) * sizeof(WCHAR));
    if( m_Buffer )
    {
    wcscpy(pstrNewString, m_Buffer);
    }
    MultiByteToWideChar(CP_ACP, 0, &ch;, 1, &pstrNewString;[m_Length], usNewLength - m_Length);
    pstrNewString[usNewLength] = L'\0';
    if( m_Buffer )
    {
    free(m_Buffer);
    }
    m_Buffer = pstrNewString;
    m_Length = usNewLength;
    m_MaximumLength = m_Length;
    }


    Reply
  • Loading, Please Wait ...

Top White Papers and Webcasts

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date