Click here to Skip to main content
15,881,424 members
Articles / Programming Languages / C++
Article

String Conversions

Rate me:
Please Sign up or sign in to vote.
4.76/5 (24 votes)
22 May 2000 293.3K   3.4K   89   25
Set of classes enabling UNICODE and ANSI string conversion.
  • Download source files - 1 Kb
  • This article describes a set of classes that are used to perform string conversions between the Unicode and ANSI character sets. Why is it needed? There are several reasons:

    1. If you are developing for COM, then all strings passed via the COM interfaces are wide strings (unless they are part of the VARIANT structure).

    2. If you are developing communication applications then (most probably) you need to send/receive data to/from the communication channel in ANSI character set (and your application is either Unicode or ANSI).
    3. If you want to have a portable application and you need to use either ANSI or Unicode strings for any reason.
    4. You are developing a DLL, service or applications that are not MFC based and you cannot use CString.

    In addition, one should consider the following:

    1. When passing wide strings via the COM interfaces, memory for these strings must be allocated with CoTaskMemAlloc() and released with CoTaskMemFree().

    2. In any other situation, it is allowed to use new and delete.

    Besides ANSI and Unicode strings, there is also a BSTR string. Microsoft class _bstr_t encapsulates the BSTR strings.

    Summary

    The small class library presented in this article contains the following classes:

    Class name

    Description

    Memory (de)allocation

    _tochar

    Convert any string to LPSTR or LPCSTR

    new/delete

    _towchar

    Convert any string to LPWSTR or LPCWSTR

    new/delete

    _totchar

    Convert any string to LPTSTR or LPCTSTR

    new/delete

    _cochar

    Convert any string to LPSTR or LPCSTR

    CoTaskMemAlloc/CoTaskMemFree

    _cowchar

    Convert any string to LPWSTR or LPCWSTR

    CoTaskMemAlloc/CoTaskMemFree

    _cotchar

    Convert any string to LPTSTR or LPCTSTR

    CoTaskMemAlloc/CoTaskMemFree

    where

    LPSTR

    char *

    Always

    LPCSTR

    const char *

    Always

    LPWSTR

    wchar_t *

    Always

    LPCWSTR

    const wchar_t *

    Always

    LPTSTR

    char *

    _UNICODE not defined

    LPTSTR

    wchar_t *

    _UNICODE defined

    LPCTSTR

    const char *

    _UNICODE not defined

    LPCTSTR

    const wchar_t *

    _UNICODE defined

    All classes are implemented inline so it is enough to include the file in your project and use the classes.

    Short Description

    All classes are designed to create a new string based on the supplied one. The buffer for the created string is automatically deleted in class destructor (unless auto delete flag is set to FALSE in the call to the constructor). This is important in COM. According to COM memory management rules, all OUT arguments must be allocated by the callee and deallocated by the caller using the standard COM memory allocator (CoTaskMemAlloc/CoTaskMemFree).

    All other classes have exactly the same interface. The only difference is the type casting operator and the type of string argument in constructor.

    For example, the _totchar class declaration looks like following:

    <FONT
        COLOR="BLUE">class</FONT> _totchar { <BR><FONT
        COLOR="BLUE">private</FONT>: <BR>    BOOL m_bAutoDelete; <BR>    LPTSTR m_tszBuffer; <BR><FONT
        COLOR="BLUE">public</FONT>: <BR>    _totchar(LPCSTR szText, BOOL bAutoDelete = TRUE);<BR>    _totchar(LPCWSTR wszText, BOOL bAutoDelete = TRUE);<BR>    ~_totchar();<BR><FONT
        COLOR="BLUE">    operator</FONT> LPTSTR();<BR><FONT
        COLOR="BLUE">    operator</FONT> LPCTSTR();<BR>}; 

    The first constructor takes a const char * argument while the second constructor takes a const wchar_t * argument. Both constructors convert the string argument to LPTSTR using "new" (the _cotchar class has exactly the same functionality but uses CoTaskMemAlloc for memory allocation).

    By default, the internal auto delete flag is set to TRUE. This means that the memory for internally created LPTSTR string will be deallocated in destructor. By setting auto delete to FALSE, memory allocated for internal LPTSTR is left intact and you have to delete it manually. This is most often used with class _cowchar because it creates a UNICODE string that can be passed via COM.

    In order to access the internal LPTSTR string, class supplies 2 type casting operators.

    Examples

    Following are several examples that use some of the classes:

    Example 1:

    You are developing a COM server and one interface method receives a string as an argument. This is the code to convert it to a portable string:

    HRESULT STDMETHODCALLTYPE <BR>IOPCGroupStateMgt::SetName( /* [string][in] */ LPCWSTR szName)<BR>{<BR>    _totchar c1(szName);<BR>    printf(_T("%s"), c1);<BR><FONT
        COLOR="BLUE">    return</FONT> S_OK;<BR>} 

    Example 2:

    You are developing a COM server and one interface method requires you to return a string as an argument.

    HRESULT STDMETHODCALLTYPE <BR><BR>IOPCGroupStateMgt::GetName( /* [string][out] */ LPWSTR *ppName)  <BR>{<BR>    LPCTSTR szName = _T("Test Name");<BR>    _cowchar c1(szName, FALSE);<BR>    ppName = c1;<BR>    <FONT
        COLOR="BLUE">return</FONT> S_OK;<BR>}

    This example converts a portable string (LPCTSTR) to Unicode string and does not deallocate memory because the converted string is assigned to an out argument of the COM interface method.

    Example 3:

    You are developing a portable application and need to send an ANSI string to serial port.

    <FONT
        COLOR="BLUE">void</FONT> TSerialPort::Write(LPCTSTR szText)<BR>{    _tochar c1(szText);<BR>    writeString(c1, strlen(c1));<BR>}<BR><BR><FONT
        COLOR="BLUE">void</FONT> TSerialPort::writeString(<FONT COLOR="BLUE">const</FONT> <FONT COLOR="BLUE">char</FONT> *szString)<BR>{<BR>    ...<BR>}

    That's all. I hope that you will find it as useful as I have.

    License

    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here


    Written By
    Web Developer SCA d.o.o.
    Serbia Serbia
    I am a cofounder of SCA Software, company that specializes in software for process control, visualization and communication. Programming for the last 10 years in C++, Delphi. Visual C++ for the last 6 years. Degree in Electronics Engineering and Telecommunications.

    Comments and Discussions

     
    Generalmay have to add _CRT_SECURE_NO_WARNINGS Pin
    hooger20174-Dec-18 16:29
    hooger20174-Dec-18 16:29 
    GeneralExcellent Pin
    Bill Erickson10-Aug-10 7:37
    Bill Erickson10-Aug-10 7:37 
    QuestionConvertion of BYTE to CString Pin
    g_satish9-Sep-09 1:04
    g_satish9-Sep-09 1:04 
    QuestionConversions string c# to string or char* C++ Pin
    wislamOS27-Jul-09 4:26
    wislamOS27-Jul-09 4:26 
    Questiontypecast CString to LPWSTR Pin
    Member 388023618-Dec-07 19:22
    Member 388023618-Dec-07 19:22 
    Generalexample to convert 'const char [16]' to 'BSTR' Pin
    vptech1925-Sep-07 2:11
    vptech1925-Sep-07 2:11 
    Generalthere is a bug while conver wchar to char Pin
    freeman chen2-Apr-07 20:38
    freeman chen2-Apr-07 20:38 
    while I convert a Chinese String(unicode) to char, an error happens.
    Because here you write in _tochar class:
    "int nLen = wcslen(wszText)+1;
    m_szBuffer = new CHAR [nLen]",
    but actually, the length of buffer will between nLen and twice as nLen if I use MBCS encoding to represent Chinese Character, so the buffer size is not larger enough, and someting unknown will happens.



    Freeman chen
    Questionhow to convert PBYTE to const char8 Pin
    mjkhan7869-Jan-07 18:34
    mjkhan7869-Jan-07 18:34 
    QuestionWhat is the difference between _TCHAR* and LPTSTR ? Pin
    ana_v12310-Jul-06 7:16
    ana_v12310-Jul-06 7:16 
    QuestionHow to convert std::string? Pin
    sfirouza17-Jul-05 5:19
    sfirouza17-Jul-05 5:19 
    AnswerRe: How to convert std::string? Pin
    kenneth nielsen31-Jul-06 4:47
    kenneth nielsen31-Jul-06 4:47 
    GeneralUnicode conversion Pin
    ditty grail24-May-05 2:07
    ditty grail24-May-05 2:07 
    General_tochar(LPCWSTR ...) bug fix Pin
    S.Cartwright6-Oct-04 14:30
    S.Cartwright6-Oct-04 14:30 
    QuestionWCHAR to mbs could have troubles no? Pin
    Michael Shiels24-Sep-04 17:21
    Michael Shiels24-Sep-04 17:21 
    AnswerRe: WCHAR to mbs could have troubles no? Pin
    Tim Smith24-Sep-04 17:32
    Tim Smith24-Sep-04 17:32 
    GeneralThank you very much ! Pin
    Jaeyoun Yi15-Jun-04 18:51
    Jaeyoun Yi15-Jun-04 18:51 
    Generalpassing stringstream to variants Pin
    Aalhad3-Nov-03 11:41
    Aalhad3-Nov-03 11:41 
    QuestionHow to convert from char* to LPSTR Pin
    Member 28151511-Mar-03 14:42
    Member 28151511-Mar-03 14:42 
    AnswerRe: How to convert from char* to LPSTR Pin
    Brian Shifrin11-Mar-03 16:25
    Brian Shifrin11-Mar-03 16:25 
    QuestionHow to convert CString to BYTE stream irrespective of UNICODE or MBCS Pin
    Hawkeye1-May-02 16:45
    Hawkeye1-May-02 16:45 
    GeneralCopy Constructors Pin
    Swinefeaster7-Feb-02 1:13
    Swinefeaster7-Feb-02 1:13 
    GeneralRe: Copy Constructors Pin
    Swinefeaster7-Feb-02 1:20
    Swinefeaster7-Feb-02 1:20 
    GeneralGood Strategy Pin
    Mike Vest29-Jun-00 4:14
    sussMike Vest29-Jun-00 4:14 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.