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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:38:35+00:00 2026-05-25T06:38:35+00:00

Basically I have a function that roughly looks like this and I need to

  • 0

Basically I have a function that roughly looks like this and I need to return out.

const char* UTF16ToUTF8(const wchar_t *in) {
    int tmp = wcslen(in);
    int size_needed = WideCharToMultiByte(CP_UTF8, 0, &in[0], (size_t)tmp, NULL, 0, NULL, NULL);
    std::vector<char> out;
    out.resize(size_needed);
    WideCharToMultiByte(CP_UTF8, 0,  &in[0], (size_t)tmp, &out[0], size_needed, NULL, NULL);

    return &out[0];
}

Obviously out gets dereferenced when returning. What are my options? I need to be able to call this function like so. I would absolutely love to stay on the stack.

utf8outputfile << UTF16ToUTF8(wchar_tString) << endl;
fprintf(utf8outputfile, "%s", UTF16ToUTF8(L"Mmm Mmm Unicode String κόσμε"));
return UTF16ToUTF8(wchar_tString);
  • 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-25T06:38:36+00:00Added an answer on May 25, 2026 at 6:38 am

    Don’t trouble yourself with any such worries and return an std::string:

    std::string UTF16ToUTF8(const wchar_t *in) {
      std::vector<char> out;
      //...
      return std::string(out.begin(), out.end());  // or std::string(out.data())
    }
    

    Then, in your C interface, use:

    printf("%s", UTF16ToUTF8(ws).c_str());
    

    I would even make the argument of the function std::wstring and extract the C-string only when calling the API function.

    The begin/end version includes all characters, the .data() version treats the buffer as a null-terminated string. Pick whichever is most appropriate.

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

Sidebar

Related Questions

I have a function that is like this - var myFunction = function(arg1, arg2,
To start out with, I basically have a function that opens up a text
So basically I have a function which is something like that : public static
I have something roughly like the following. Basically I need to access the class
I have a function that basically does: internal static short GetPresentYear() { return Convert.ToInt16(DateTime.Now.Year
I basically have something like this: void Foo(Type ty) { var result = serializer.Deserialize<ty>(inputContent);
Okay i've seen this done somewhere before where you have a function that takes
I basically have an array of 50 integers, and I need to find out
I basically have an object, extended with a function through its prototype. Inside that
I have a table that looks like id Type: Auto inc-int Comment: the unique

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.