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

Threaded View

  1. #1
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    [RESOLVED] Unicode string conversion function issue

    Hey.

    For some reason, wstr ends up being an empty string. I've debugged it and it the conversion functions works, but it seems that when it returns the string something goes wrong:
    Code:
    LPCWSTR wstr = Conversions::to_wstring("hello").c_str();
    Code:
    std::wstring Conversions::to_wstring(char* mb_str)
    {
        const size_t old_size = strlen(mb_str) + 1;
        const size_t new_size = old_size * 2;
        size_t converted_chars = 0;
        boost::scoped_array<wchar_t> wc_string(new wchar_t&#091;new_size&#093;);
    
        mbstowcs_s(&converted_chars, wc_string.get(), old_size, mb_str, _TRUNCATE);
    
        return std::wstring(wc_string.get());
    }
    
    std::wstring Conversions::to_wstring(const char* mb_str)
    {
        return to_wstring(const_cast<char*>(mb_str));
    }
    Also, what would be a more portable way to determine the size of new_size in Conversions::to_wstring?

    Cheers.
    Last edited by Mybowlcut; June 28th, 2009 at 03:58 AM.
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

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