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 6136993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:40:20+00:00 2026-05-23T17:40:20+00:00

How can i convert a narrow string to a wide string ? I have

  • 0

How can i convert a narrow string to a wide string ?

I have tried this method :

string myName;
getline( cin , myName );
wstring printerName( L(myName) );  // error C3861: 'L': identifier not found
wchar_t* WprinterName = printerName.c_str(); // error C2440: 'initializing' : cannot convert from 'const wchar_t *' to 'wchar_t *'

But i get errors as listed above.

Why do i get these errors ? How can i fix them ?

Is there any other method of directly converting a narrow string to a wide string ?

  • 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-23T17:40:21+00:00Added an answer on May 23, 2026 at 5:40 pm

    You should do this :

    inline std::wstring convert( const std::string& as )
    {
                // deal with trivial case of empty string
        if( as.empty() )    return std::wstring();
    
                // determine required length of new string
        size_t reqLength = ::MultiByteToWideChar( CP_UTF8, 0, as.c_str(), (int)as.length(), 0, 0 );
    
                // construct new string of required length
        std::wstring ret( reqLength, L'\0' );
    
                // convert old string to new string
        ::MultiByteToWideChar( CP_UTF8, 0, as.c_str(), (int)as.length(), &ret[0], (int)ret.length() );
    
                // return new string ( compiler should optimize this away )
        return ret;
    }
    

    This expects the std::string to be UTF-8 (CP_UTF8), when you have another encoding replace the codepage.

    Another way could be :

    inline std::wstring convert( const std::string& as )
    {
        wchar_t* buf = new wchar_t[as.size() * 2 + 2];
        swprintf( buf, L"%S", as.c_str() );
        std::wstring rval = buf;
        delete[] buf;
        return rval;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I have access to UrlHelper I can convert an ActionResult to a string
In MSSQL you can convert a string into an integer like this: CONVERT(INT, table.column)
You can convert a negative number to positive like this: int myInt = System.Math.Abs(-5);
How can I convert a number contained in a string from any base to
I know you can convert a String to an number with read : Prelude>
In Specman I can convert a variable to a string using either: x.to_string(); or
The SQL Server convert () function can convert varbinary data to a string with
I have some code that can convert a single color in a template image
I converted a narrow string to the wide string as follows : string nameOfPrinter;
I can convert an integer into string using String s = + 4; //

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.