Open In App

Convert LPCWSTR to std::string in C++

Last Updated : 29 Aug, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In C++, strings can be represented using different character encodings. LPCWSTR stands for Long Pointer to Constant Wide STRing. The LPCWSTR type represents a wide string using the UTF-16 encoding, while the std::string type typically represents a narrow string using the UTF-8 encoding. Converting between these two types requires careful handling of character encodings to ensure data integrity.

In this article, we’ll explore three different approaches to convert an LPCWSTR to a std::string in C++.

1. Using WideCharToMultiByte() Function

The WideCharToMultiByte is a function in Windows API that converts a wide-character string to a multibyte string. It provides a convenient way to convert LPCWSTR to std::string.

Example

The below C++ program demonstrates the usage of the WideCharToMultiByte function to convert LPCWSTR to a multibyte character string (LPSTR).

C++




// C++ program that demonstrates the usage of
// WideCharToMultiByte function to convert
// LPCWSTR to std::string
#include <Windows.h>
#include <iostream>
#include <string>
using namespace std;
  
// Function to convert LPCWSTR to std::string
string ConvertLPCWSTRToString(LPCWSTR lpcwszStr)
{
    // Determine the length of the converted string
    int strLength
        = WideCharToMultiByte(CP_UTF8, 0, lpcwszStr, -1,
                              nullptr, 0, nullptr, nullptr);
  
    // Create a std::string with the determined length
    string str(strLength, 0);
  
    // Perform the conversion from LPCWSTR to std::string
    WideCharToMultiByte(CP_UTF8, 0, lpcwszStr, -1, &str[0],
                        strLength, nullptr, nullptr);
  
    // Return the converted std::string
    return str;
}
  
int main()
{
    // Example LPCWSTR
    LPCWSTR wideStr = L"GeeksforGeeks";
  
    // Convert LPCWSTR to std::string
    string str = ConvertLPCWSTRToString(wideStr);
  
    // Print the converted string
    cout << str << endl;
  
    return 0;
}


Output

GeeksforGeeks

Explanation

  • The WideCharToMultiByte function determines the length of the converted string using the WideCharToMultiByte function with a null lpMultiByteStr parameter and a dwFlags value of 0.
  • A std::string object is created with the determined length, initializing it with null characters.
  • The WideCharToMultiByte function is called again, this time with the target std::string object passed as the lpMultiByteStr parameter to store the converted string.

2. Using std::wstring_convert (C++11 or later)

Starting from C++11, the <codecvt> header introduced the std::wstring_convert class template, which can be used to convert between wide and narrow character strings.

Example

The code demonstrates a simple way to convert a wide-character string to a std::string using the wstring_convert class template and the codecvt_utf8_utf16 codecvt facet.

C++




// C++ code that demonstrates a simple way to convert a wide
// character string to a std::string using the
// wstring_convert class template and the codecvt_utf8_utf16
// codecvt facet.
  
#include <codecvt>
#include <iostream>
#include <locale>
#include <string>
#include <windows.h>
using namespace std;
  
// Function to convert LPCWSTR to std::string
string ConvertLPCWSTRToString(const LPCWSTR lpcwszStr)
{
    // Create a converter object to convert between wide
    // strings and UTF-8 encoded strings
    wstring_convert<codecvt_utf8_utf16<wchar_t> > converter;
  
    // Create a wstring from the LPCWSTR input
    wstring wstr(lpcwszStr);
  
    // Convert the wide string to an std::string
    return converter.to_bytes(wstr);
}
  
int main()
{
    // Example LPCWSTR
    LPCWSTR wideStr = L"GeeksforGeeks";
  
    // Convert LPCWSTR to std::string
    string str = ConvertLPCWSTRToString(wideStr);
  
    // Print the converted string
    cout << str << endl;
  
    return 0;
}


Output

GeeksforGeeks

Explanation

  • A std::wstring_convert object is instantiated with the std::codecvt_utf8_utf16<wchar_t> facet. This facet handles the conversion between UTF-16 and UTF-8 character encodings.
  • The LPCWSTR input is converted to a std::wstring object.
  • The to_bytes member function of the std::wstring_convert object converts the std::wstring to a std::string using the specified encoding.

3. Using std::wstring and std::wstring_convert (C++17 or later)

Starting from C++17, the <string> header introduced new overloads of std::string constructors and member functions that accept wide-character string inputs. Combined with std::wstring_convert, this provides an alternative approach.

Example

  • The code demonstrates an alternative approach to convert a wide-character string to a std::string using the wstring_convert class template and the codecvt_utf8_utf16 codecvt facet.
  • Instead of creating a std::wstring object separately, it directly converts the LPCWSTR to a std::string by constructing a temporary wstring object within the converter.to_bytes function call.
  • This saves a step in the conversion process.

Implementation:

C++




#include <codecvt>
#include <iostream>
#include <locale>
#include <string>
#include <windows.h>
using namespace std;
  
// Function to convert LPCWSTR to std::string
string ConvertLPCWSTRToString(LPCWSTR lpcwszStr)
{
    // Create a converter object to convert between wide
    // strings and UTF-8 encoded strings
    wstring_convert<codecvt_utf8_utf16<wchar_t> > converter;
  
    // Convert the LPCWSTR to a wstring and then to an
    // std::string
    return converter.to_bytes(wstring(lpcwszStr));
}
  
int main()
{
    // Example LPCWSTR
    LPCWSTR wideStr = L"GeeksforGeeks";
  
    // Convert LPCWSTR to std::string
    string str = ConvertLPCWSTRToString(wideStr);
  
    // Print the converted string
    cout << str << endl;
  
    return 0;
}


Output

GeeksforGeeks

Explanation

  • The LPCWSTR input is directly converted to a std::wstring object.
  • The std::wstring object is then passed to the to_bytes member function of the std::wstring_convert object, which converts it to a std::string using the specified encoding.

Conclusion

In this article, we explored three different approaches to convert an LPCWSTR to a std::string in C++. The first method uses the Windows API function WideCharToMultiByte, which is suitable for Windows-specific code. The second and third methods leverage the std::wstring_convert class template to perform the conversion, providing a more portable solution. Depending on your C++ version and requirements, you can choose the method that best fits your needs.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads