CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Mar 2010
    Posts
    1

    Converting wchar_t* to char*

    Hello fellas,

    I have encountered a problem during the converting process.
    Here below is my function to convert wchar_t * to char*

    void wtoc(char* Dest,const wchar_t *Source)
    {
    int i = 0;

    while(Source[i] != '\0')
    {
    Dest[i] = (char)Source[i];
    ++i;
    }
    }

    it compiles succesfully however on linking phase, it gives out an error


    undefined reference to `mynamespace::myfunc::wtoc(char*, wchar_t const*)'


    and it is really confusing because I don't have a function with a prototype like that. my prototype is

    void wtoc(char* Dest,const wchar_t *Source)

    so what is wrong with me in here? And also can you suggest any alternative ways to convert wchar_t to char

    Thanx in advance.
    DK

  2. #2
    Join Date
    Jan 2009
    Posts
    1,689

    Re: Converting wchar_t* to char*

    My guess is that the function you posted is below where you call it. Try declaring it at the top of your program, if that doesn't work, show us the whole program.

  3. #3
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: Converting wchar_t* to char*

    Have you tried something like the following?

    Code:
    void mynamespace::wtoc(char* Dest,const wchar_t *Source)
    {
    int i = 0;
    
    while(Source[i] != '\0')
    {
    Dest[i] = (char)Source[i];
    ++i;
    }
    }
    My hobby projects:
    www.rclsoftware.org.uk

  4. #4
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: Converting wchar_t* to char*

    Quote Originally Posted by vurgac View Post
    Code:
    void wtoc(char* Dest,const wchar_t *Source)
    {
        int i = 0;
    
        while(Source[i] != '\0')
        {
            Dest[i] = (char)Source[i];
            ++i;
        }
    }
    As Zaccheus suggested, you have probably declared your function in the [nested] namespace, but defined it as a global.
    Off-top:
    1. Your destination string is NOT zero-terminated
    2. I don't think this is how you convert wide char to char
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  5. #5
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Converting wchar_t* to char*

    Quote Originally Posted by VladimirF View Post
    2. I don't think this is how you convert wide char to char
    The precise conversion is dependent on the source and destination encoding, of course, which is distinct from but related to the char size. So long as only (7-bit) ASCII text is present and the wide characters use UCS-2 or UTF-16, simply casting to char should work. A full conversion from UTF-16 to UTF-8 would require more complex logic, of course.

  6. #6
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: Converting wchar_t* to char*

    Quote Originally Posted by Lindley View Post
    The precise conversion is dependent on the source and destination encoding, of course, which is distinct from but related to the char size. So long as only (7-bit) ASCII text is present and the wide characters use UCS-2 or UTF-16, simply casting to char should work. A full conversion from UTF-16 to UTF-8 would require more complex logic, of course.
    This statement is similar to:
    Code:
    void longtoc(char* Dest,const long Source)
    {
        *Dest = (char)Source;
    }
    function will convert long value to char, provided that only 7-bit values were used
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Converting wchar_t* to char*

    Basically, yeah.

  8. #8
    Join Date
    Mar 2010
    Posts
    11

    Re: Converting wchar_t* to char*

    To properly convert wchar_t to char you may use wcsrtombs() standard function. You may specify any encoding to use and the function is completely safe.

Tags for this Thread

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