Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8361221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:39:02+00:00 2026-06-09T11:39:02+00:00

As title states, I need a way to convert a PWCHAR to a std::string.

  • 0

As title states, I need a way to convert a PWCHAR to a std::string. The only solutions I can find online are for the opposite conversion, so I’d really like it if someone could shed some light on this. Thanks!

This is in c++.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T11:39:03+00:00Added an answer on June 9, 2026 at 11:39 am

    According to this MSDN page, PWCHAR is declared as follows:

    typedef wchar_t WCHAR, *PWCHAR;
    

    What you want is std::wstring, declared in string.

    const PWCHAR pwc;
    std::wstring str(pwc);
    

    std::wstring is very similar to std::string, as both are specializations of std::basic_string; the difference is in that wstring uses wchar_t (Windows WCHAR), whereas string uses char.


    If you truly want a string (and not a wstring), the advised C++ way is to use use_facet as seen here:

    const std::locale locale("C");
    const std::wstring src = ...;
    const std::string dst = ...;
    std::use_facet<std::ctype<wchar_t> >(loc)
        .narrow(src.c_str(), src.c_str() + src.size(), '?', &*dst.begin());
    

    You may also separately convert to a multibyte C string and then use this to build your std::string. This is not the preferred way of doing this in C++, however. The function for doing this is wcstombs, as declared below:

    size_t wcstombs ( char * mbstr, const wchar_t * wcstr, size_t max );
    

    Since you’re on Windows, you may also use WideCharToMultiByte for this step.

    int WideCharToMultiByte(
      __in       UINT CodePage,
      __in       DWORD dwFlags,
      __in       LPCWSTR lpWideCharStr,
      __in       int cchWideChar,
      __out_opt  LPSTR lpMultiByteStr,
      __in       int cbMultiByte,
      __in_opt   LPCSTR lpDefaultChar,
      __out_opt  LPBOOL lpUsedDefaultChar
    );
    

    LPSTR is defined as follows according to the MSDN:

    typedef CHAR *LPSTR;
    typedef char CHAR;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

As the title states, I need to relace all occurrences of the $ sign
Exactly like my title states i need a query that removes the first chracter
I cant find the way to fetch only necessary fields of model in certain
As title states, if I have a float, I need to get the fraction
As the title states, I am looking for a java collection keeping only the
As the title states, I am using devise. I came upon this link that
As the title states, if I don't clean my project before building, neither the
As the title states, I'm trying to resize a Texture2D before even considering SpriteBatch.Draw()
As the title states, how expensive are Python dictionaries to handle? Creation, insertion, updating,
As the title states my Ajax call is actually causing the form to be

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.