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

  • Home
  • SEARCH
  • 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 6364089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:07:12+00:00 2026-05-25T00:07:12+00:00

How can I compare a wstring , such as LHello , to a string

  • 0

How can I compare a wstring, such as L"Hello", to a string? If I need to have the same type, how can I convert them into the same type?

  • 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-05-25T00:07:12+00:00Added an answer on May 25, 2026 at 12:07 am

    Since you asked, here’s my standard conversion functions from string to wide string, implemented using C++ std::string and std::wstring classes.

    First off, make sure to start your program with set_locale:

    #include <clocale>
    
    int main()
    {
      std::setlocale(LC_CTYPE, "");  // before any string operations
    }
    

    Now for the functions. First off, getting a wide string from a narrow string:

    #include <string>
    #include <vector>
    #include <cassert>
    #include <cstdlib>
    #include <cwchar>
    #include <cerrno>
    
    // Dummy overload
    std::wstring get_wstring(const std::wstring & s)
    {
      return s;
    }
    
    // Real worker
    std::wstring get_wstring(const std::string & s)
    {
      const char * cs = s.c_str();
      const size_t wn = std::mbsrtowcs(NULL, &cs, 0, NULL);
    
      if (wn == size_t(-1))
      {
        std::cout << "Error in mbsrtowcs(): " << errno << std::endl;
        return L"";
      }
    
      std::vector<wchar_t> buf(wn + 1);
      const size_t wn_again = std::mbsrtowcs(buf.data(), &cs, wn + 1, NULL);
    
      if (wn_again == size_t(-1))
      {
        std::cout << "Error in mbsrtowcs(): " << errno << std::endl;
        return L"";
      }
    
      assert(cs == NULL); // successful conversion
    
      return std::wstring(buf.data(), wn);
    }
    

    And going back, making a narrow string from a wide string. I call the narrow string “locale string”, because it is in a platform-dependent encoding depending on the current locale:

    // Dummy
    std::string get_locale_string(const std::string & s)
    {
      return s;
    }
    
    // Real worker
    std::string get_locale_string(const std::wstring & s)
    {
      const wchar_t * cs = s.c_str();
      const size_t wn = std::wcsrtombs(NULL, &cs, 0, NULL);
    
      if (wn == size_t(-1))
      {
        std::cout << "Error in wcsrtombs(): " << errno << std::endl;
        return "";
      }
    
      std::vector<char> buf(wn + 1);
      const size_t wn_again = std::wcsrtombs(buf.data(), &cs, wn + 1, NULL);
    
      if (wn_again == size_t(-1))
      {
        std::cout << "Error in wcsrtombs(): " << errno << std::endl;
        return "";
      }
    
      assert(cs == NULL); // successful conversion
    
      return std::string(buf.data(), wn);
    }
    

    Some notes:

    • If you don’t have std::vector::data(), you can say &buf[0] instead.
    • I’ve found that the r-style conversion functions mbsrtowcs and wcsrtombs don’t work properly on Windows. There, you can use the mbstowcs and wcstombs instead: mbstowcs(buf.data(), cs, wn + 1);, wcstombs(buf.data(), cs, wn + 1);

    In response to your question, if you want to compare two strings, you can convert both of them to wide string and then compare those. If you are reading a file from disk which has a known encoding, you should use iconv() to convert the file from your known encoding to WCHAR and then compare with the wide string.

    Beware, though, that complex Unicode text may have multiple different representations as code point sequences which you may want to consider equal. If that is a possibility, you need to use a higher-level Unicode processing library (such as ICU) and normalize your strings to some common, comparable form.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string +1.29% or -1.29% How can I convert this into a
I know you can compare the length but many hash types have the same
I have an IEnumerable, listOfOnes, and an IEnumerable, listOfTwos. Assuming that I can compare
Do you have any experience with T4 and T4 Editor ? Can you compare
How I can compare two IP address? string ip1 = 123.123.123.123; string ip2 =
I need to get a label during runtime so that i can compare to
All I need is to compare dates represented by string values in the format
In Informix, how can I cast a char(8) type into a money type, so
I have a question in Django on how you can compare dates to solve
I'd appreciate any idea on how to do it, so we can compare them

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.