wcsrtombs_s question

This is a discussion on wcsrtombs_s question within the C++ Programming forums, part of the General Programming Boards category; OK I'm using VC++ and I've read the MSDN description at wcsrtombs_s but I've obviously misunderstood something. The following function ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    46

    wcsrtombs_s question

    OK I'm using VC++ and I've read the MSDN description at
    wcsrtombs_s
    but I've obviously misunderstood something.

    The following function is supposed to produce a popup window with the message given in 'str'. When compiled as a Unicode program the wide string str needs to be converted into a UTF-8 string for fl_message. What I actually get is a null string for 'copiedstring'.

    Possibly this has something to do with the setlocale function?

    Any hints gratefully received.

    Code:
    void pbox(const _TCHAR *str)
    { 
    	char copiedstring[250];
    	mbstate_t mbstate;
    	size_t retval;
    #ifdef _UNICODE
    //	setlocale(LC_CTYPE, "en_US.UTF-8"); 
    	// Convert to UTF-8 from wchar_t
        // Reset to initial conversion state
    	const wchar_t *wcsIndirectString = str;
        memset(&mbstate, 0, sizeof(mbstate));
    	wcsrtombs_s(&retval, copiedstring, sizeof(copiedstring), &str, 249, &mbstate);
    	fl_message(copiedstring);
    #else
    	fl_message(str);
    #endif
    }

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    46
    Quote Originally Posted by PaulBlay View Post
    Possibly this has something to do with the setlocale function?
    Well on further checking I noticed this ...

    The set of available languages, country/region codes, and code pages includes all those supported by the Win32 NLS API except code pages that require more than two bytes per character, such as UTF-7 and UTF-8. If you provide a code page like UTF-7 or UTF-8, setlocale will fail, returning NULL. The set of language and country/region codes supported by setlocale is listed in Language and Country/Region Strings.
    So how am I supposed to convert a wchar_t string to a UTF-8 string?

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,662
    Some code I have laying around...
    Code:
    std::wstring utf8_to_wstr(const std::string &utf8)
    {
        // NOTE: we are assuming that the number of bytes in a UTF8 string is 
        //       always >= to the number of wchar_t's required to represent that 
        //       string in UTF16LE - which should hold true
        std::vector<wchar_t> wbuff(utf8.length() + 1);
    
        // NOTE: this does not NULL terminate the string in wbuff, but this is ok
        //       since it was zero-initialized in the vector constructor
        if (!MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(), utf8.length(),
                                 &wbuff[0], utf8.length()))
        {
            return L"ErrorW";
        }//if
    
        return &wbuff[0];
    }//utf8_to_wstr
    
    std::string wstr_to_str(const std::wstring &wstr, UINT cp = CP_ACP)
    {
        int len = WideCharToMultiByte(cp, 0, wstr.c_str(), wstr.length(), 
                                      0, 0, 0, 0);
        if (!len)
            return "ErrorA";
    
        std::vector<char> abuff(len + 1);
    
        // NOTE: this does not NULL terminate the string in abuff, but this is ok
        //       since it was zero-initialized in the vector constructor
        if (!WideCharToMultiByte(cp, 0, wstr.c_str(), wstr.length(), 
                                 &abuff[0], len, 0, 0))
        {
            return "ErrorA";
        }//if
    
        return &abuff[0];
    }//wstr_to_str
    gg

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    46
    Quote Originally Posted by Codeplug View Post
    Some code I have laying around...
    Great. Would you mind me using that, with attribution, in a GPL licensed project?

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,662
    >> Would you mind me using that
    Not at all.

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21