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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:19:25+00:00 2026-06-13T06:19:25+00:00

While developing a program the other day, I had to convert an ASCII string

  • 0

While developing a program the other day, I had to convert an ASCII string into a Unicode string. I’m working on Windows with Visual Studio 2012, by the way. I noticed some strange behaviour with the Win32 function MultiByteToWideChar which I couldn’t sort out. I wrote some test code, below:

int main()
{
    /* Create const test string */
    char str[] = "test string";

    /* Create empty wchar_t buffer to hold Unicode form of above string, and initialize (zero) it */
    wchar_t *buffer = (wchar_t*) LocalAlloc(LMEM_ZEROINIT, sizeof(wchar_t) * strlen(str));

    /* Convert str to Unicode and store in buffer */
    int result = MultiByteToWideChar(CP_UTF8, NULL, str, strlen(str), buffer, strlen(str));
    if (result == 0)
        printf("GetLastError result: %d\n", GetLastError());

    /* Print MultiByteToWideChar result, str's length, and buffer's length */
    printf_s(
        "MultiByteToWideChar result: %d\n"
        "'str' length: %d\n"
        "'buffer' length: %d\n",
        result, strlen(str), wcslen(buffer));

    /* Create a message box to display the Unicode string */
    MessageBoxW(NULL, buffer, L"'buffer' contents", MB_OK);

    /* Also write buffer to file, raw */
    FILE *stream = NULL;
    fopen_s(&stream, "c:\\test.dat", "wb");
    fwrite(buffer, sizeof(wchar_t), wcslen(buffer), stream);
    fclose(stream);

    return 0;
}

As you can see, it simply takes an ordinary character string, creates a buffer to store the Unicode string in, puts that converted Unicode string into the buffer, and shows me some results, also writing the buffer to a file.

The output:

MultiByteToWideChar result: 11
'str' length: 11
'buffer' length: 16

Already weird. The function is processing the correct number of characters in the C string, but wcslen is reporting the output buffer to be longer than the C string! I’m pretty sure I allocated the buffer correctly, too.

I’ve tried using different sized string lengths, but there’s always junk at the end, and wcslen always reports the buffer’s length to be a multiple of 4.

Finally, for this particular string ("test string"), here’s the raw buffer that was printed to file:

74 00 65 00 73 00 74 00 20 00 73 00 74 00 72 00   t.e.s.t. .s.t.r.
69 00 6E 00 67 00 AB AB AB AB AB AB AB AB EE FE   i.n.g...........

(That’s 32 bytes, or 16 Unicode characters.)

The 10 bytes at the end are five characters; four U+ABAB, and one U+FEEE, which are meaningless to me.

In different amounts, they occur every time I try converting a string.

I’m kinda out of ideas. Anyone?

Thanks in advance!

  • 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-13T06:19:26+00:00Added an answer on June 13, 2026 at 6:19 am
    /* Create empty wchar_t buffer to hold Unicode form of above string, and initialize (zero) it */
    wchar_t *buffer = (wchar_t*) LocalAlloc(LMEM_ZEROINIT, sizeof(wchar_t) * strlen(str));
    

    This is really where the problem started. The value of strlen(str) is meaningless, especially so when the input string is encoded in utf-8. You tend to get away with it by accident because it usually creates a buffer that’s too long, not counting the off-by-one bug.

    But you would also have easily avoided that bug by doing it the Right Way. You must call the function twice. The first time, pass 0 for the last argument (cchWideChar). The function returns the required size of the buffer (chars, not bytes). Which is now good enough to allocate the buffer and pass the correct value the second time you call the function.

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

Sidebar

Related Questions

VB.NET program. While developing on Visual Studio Express 2010, there are two buttons: Start
While developing AJAX program, I met the design decision to make string I18N in
While developing an application, I had the following problem. I wanted to return an
While developing a Launcher (Homescreen) application for Android, I've come into a security exception
A sselenium program I have been developing for a while now comes up with
I'm developing an file management Windows application. The program should keep an array of
I am developing a http program in C# while referring to some source code
I am developing a program which uses visual styles. The Main method looks like
Just a wondering while developing a network traffic inspection program, no context is relevant!
While developing an application using gwt in ecliplse crashed. Now the server is running

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.