CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: WCHAR* to CHAR*

Threaded View

  1. #3
    Join Date
    Feb 2004
    Location
    Texas, USA
    Posts
    1,206

    Re: WCHAR* to CHAR*

    when using wstring and string, do I have to use transform?

    I guess I failed to offer more specific details concerning my problem.

    I'm dealing with a structure that, when returned from a function, contains a member with the data type of PWSTR. I need to convert this string in this structure to a CHAR*, so that I can output it using std::cout.

    In your examples, you know the size of the WCHAR*, but in my case I don't know the size of the string that I need to convert, so it would be impossible to use std::transform without the appropriate iterators.

    **EDIT**

    I fixed it, thanks for the help though.

    Here is what I have. These functions assume the destination buffer is large enough to contain the source after conversion. Buffer overrun errors could occur if the assumption is proven false:

    Code:
    //=====================================================================================
    /*
    || ::DESCRIPTION::
    || This function will convert a WCHAR string to a CHAR string.
    ||
    || Param 1 :: Pointer to a buffer that will contain the converted string. Ensure this
    ||            buffer is large enough; if not, buffer overrun errors will occur.
    || Param 2 :: Constant pointer to a source WCHAR string to be converted to CHAR
    */
    void wtoc(CHAR* Dest, const WCHAR* Source)
    {
        int i = 0;
    
        while(Source[i] != '\0')
        {
            Dest[i] = (CHAR)Source[i];
            ++i;
        }
    }
    
    //=====================================================================================
    /*
    || ::DESCRIPTION::
    || This function will convert a CHAR string to a WCHAR string.
    ||
    || Param 1 :: Pointer to a buffer that will contain the converted string. Ensure this
    ||            buffer is large enough; if not, buffer overrun errors will occur.
    || Param 2 :: Constant pointer to a source CHAR string to be converted to WCHAR
    */
    void ctow(WCHAR* Dest, const CHAR* Source)
    {
        int i = 0;
    
        while(Source[i] != '\0')
        {
            Dest[i] = (WCHAR)Source[i];
            ++i;
        }
    }
    Last edited by MrDoomMaster; April 9th, 2005 at 01:18 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured