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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:26:07+00:00 2026-06-15T23:26:07+00:00

I am facing weird error while printing out byte representation of std::string while std::wstring

  • 0

I am facing weird error while printing out byte representation of std::string while std::wstring works fine.

std::string str = "mystring";
unsigned short* vtemp = (unsigned short*)str.c_str();
for(int i=0; i<str.length(); ++i)
{
    cout << (unsigned short)((unsigned char)vtemp[i]) << " ";
}
cout << endl;

Incorrect Output: 109 115 114 110 0 204 204 204


wstring wstr(str.length(), L' ');
std::copy(str.begin(), str.end(), wstr.begin());

vtemp = (unsigned short*)wstr.c_str();

for(int i=0; i<wstr.length(); ++i)
{
    cout << (unsigned short)((unsigned char)vtemp[i]) << " ";
}
cout << endl;

Correct Output: 109 121 115 116 114 105 110 103

In first case, every alternate character was skipped. Why so?

This program was run on windows with unicode character set enabled in project settings.

  • 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-15T23:26:08+00:00Added an answer on June 15, 2026 at 11:26 pm

    It is because of this line:

    unsigned short* vtemp = (unsigned short*)str.c_str();
    

    unsigned short is two bytes long. char is one byte long. You are setting an unsigned short pointer to a char array and iterating by pointer indexing (every two bytes).

    The compiler would normally tell you that, but your usage of C-style casts prevents that (because C-style casts fail silently).

    Later edit: Your code also indexes an unsigned short* up to str.length() elements, but (the short being bigger than char) your array only contains str.length() / 2 unsigned short indexable elements.

    Running that code on some machines will probably result in a core-dump.

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

Sidebar

Related Questions

I am facing a weird error while printing one of my Link List named
I'm facing some weird css issue with django . My css works fine but
I am facing a weird issues while fetching base64 encoded string sent from client
I am facing a weird error in my code while using HAML where my
I am facing a really weird error,while trying to insert into a table called
I am facing a weird error.I have set up my code in IntelliJ Idea.
I'm currently facing a weird problem while executing a command from my bash script.
I am facing a weird problem. While browsing the C code of a project,
I am facing a weird issue: My source string for Html.fromHtml() is as follows:
I am facing a weird problem. My web application throws error Value to add

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.