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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T10:37:32+00:00 2026-05-12T10:37:32+00:00

Most texts on the C++ standard library mention wstring as being the equivalent of

  • 0

Most texts on the C++ standard library mention wstring as being the equivalent of string, except parameterized on wchar_t instead of char, and then proceed to demonstrate string only.

Well, sometimes, there are some specific quirks, and here is one: I can’t seem to assign a wstring from an NULL-terminated array of 16-bit characters. The problem is the assignment happily uses the null character and whatever garbage follows as actual characters. Here is a very small reduction:

typedef unsigned short PA_Unichar;
PA_Unichar arr[256];
fill(arr); // sets to 52 00 4b 00 44 00 61 00 74 00 61 00 00 00 7a 00 7a 00 7a 00
// now arr contains "RKData\0zzz" in its 10 first values
wstring ws;
ws.assign((const wchar_t *)arr);
int l = ws.length();

At this point l is not the expected 6 (numbers of chars in “RKData”), but much larger. In my test run, it is 29. Why 29? No idea. A memory dump doesn’t show any specific value for the 29th character.

So the question: is this a bug in my standard C++ library (Mac OS X Snow Leopard), or a bug in my code?
How am I supposed to assign a null-terminated array of 16-bit chars to a wstring?

Thanks

  • 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-12T10:37:32+00:00Added an answer on May 12, 2026 at 10:37 am

    Under most Unixes (Mac OS X as well), whar_t represents UTF-32 single code point, and not 16bit utf-16 point like at windows.

    So you need to:

    1. Either:

      ws.assing(arr,arr + length_of_string);
      

      That would use arr as iterator and copy each short int to wchar_t.
      But this would work only if your characters lay in BMP or representing UCS-2
      (16bit legacy encoding).

    2. Or, correctly work with utf-16: converting utf-16 to utf-32 — you need to find surrogate pairs and merge them to single code point.

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

Sidebar

Related Questions

Using the sqlite3 standard library in python 2.6.4, the following query works fine on
I have built a library that dumps most of its debug text using Console.WriteLine();
I was surprised to see an example of a string being initialised to null
It's standard in most modern editors that you can highlight a piece of code
From http://www.cplusplus.com/reference/iostream/cout/ : By default, most systems have their standard output set to the
I need to validate various ASP.NET controls, but instead of displaying the standard text/asterisk/image
I'm currently building tests using Capybara/Rails, and want to know the most reliable/standard way
There is a directory in the standard ASP.NET template Content where most people seem
I notice that most android apps have some sort of standard in that they
How do I transform this JSON object: context:{#text:[Most Visited Pages , Hall Residents Advanced

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.