Click here to Skip to main content
Click here to Skip to main content

String Conversions

, 22 May 2000
Rate this:
Please Sign up or sign in to vote.
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

    About the Author

    Zoran M. Todorovic
    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

     
    GeneralExcellent PinmemberBill Erickson10-Aug-10 7:37 
    QuestionConvertion of BYTE to CString Pinmemberg_satish9-Sep-09 1:04 
    QuestionConversions string c# to string or char* C++ PinmemberwislamOS27-Jul-09 4:26 
    Questiontypecast CString to LPWSTR PinmemberMember 388023618-Dec-07 19:22 
    Generalexample to convert 'const char [16]' to 'BSTR' Pinmembervptech1925-Sep-07 2:11 
    Generalthere is a bug while conver wchar to char Pinmemberfreeman chen2-Apr-07 20:38 
    Questionhow to convert PBYTE to const char8 PinmemberMember #34578799-Jan-07 18:34 
    QuestionWhat is the difference between _TCHAR* and LPTSTR ? Pinmemberana_v12310-Jul-06 7:16 
    QuestionHow to convert std::string? Pinmembersfirouza17-Jul-05 5:19 
    AnswerRe: How to convert std::string? Pinmemberkenneth nielsen31-Jul-06 4:47 
    GeneralUnicode conversion Pinmemberditty grail24-May-05 2:07 
    General_tochar(LPCWSTR ...) bug fix Pinmembersammyc6-Oct-04 14:30 
    QuestionWCHAR to mbs could have troubles no? PinmemberMichael Shiels24-Sep-04 17:21 
    AnswerRe: WCHAR to mbs could have troubles no? PinmemberTim Smith24-Sep-04 17:32 
    GeneralThank you very much ! PinmemberJaeyoun Yi15-Jun-04 18:51 
    Generalpassing stringstream to variants PinmemberAalhad3-Nov-03 11:41 
    QuestionHow to convert from char* to LPSTR PinmemberRaphael.Woo@aussoft.com11-Mar-03 14:42 
    AnswerRe: How to convert from char* to LPSTR PinmemberBrian Shifrin11-Mar-03 16:25 
    QuestionHow to convert CString to BYTE stream irrespective of UNICODE or MBCS PinmemberHawkeye1-May-02 16:45 
    GeneralCopy Constructors Pinmemberswinefeaster7-Feb-02 1:13 
    GeneralRe: Copy Constructors Pinmemberswinefeaster7-Feb-02 1:20 
    GeneralGood Strategy PinsussMike Vest29-Jun-00 4:14 

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

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

    | Advertise | Privacy | Mobile
    Web01 | 2.8.140721.1 | Last Updated 23 May 2000
    Article Copyright 2000 by Zoran M. Todorovic
    Everything else Copyright © CodeProject, 1999-2014
    Terms of Service
    Layout: fixed | fluid