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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:54:47+00:00 2026-05-27T12:54:47+00:00

Why do char arrays stop right before a 0x00 byte is detected and how

  • 0

Why do char arrays stop right before a 0x00 byte is detected and how can this problem be avoided (perhaps by using another datatype (which one and why) or a “trick” with char)?

For example in the following code, the output is “a” only, the other bytes are not displayed:

unsigned char cbuffer[]={0x61,0x00,0x62,0x63,0x0};
std::string sbuffer=reinterpret_cast<const char*>(cbuffer);

cout << sbuffer << endl;

Similarly in the following code, the output is “ab”:

unsigned char cbuffer[]={0x61,0x62,0x00,0x63,0x0};
std::string sbuffer=reinterpret_cast<const char*>(cbuffer);

Straightforward and effective workarounds to the problem (where 0x00 is kept in the array as a normal byte) would be appreciated.

  • 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-27T12:54:48+00:00Added an answer on May 27, 2026 at 12:54 pm

    It’s common in C to pass around strings as pointers to null-terminated char arrays. null is represented by 0x00. To make conversion easy, the std::string is constructable from a pointer to a null-terminated char array, which is what is happening with your code. But when it finds the null, it thinks that’s the end of the string. If you cout a char array directly, you’ll find it makes the same assumption, because they have no other way to determine the end of a string pointed to by a char*. (They could theoretically tell the length in your case, if they understood char (&)[], but almost nothing in the standard library does sadly).

    The intended workarounds are to use this constructor instead:

    int len = sizeof(cbuffer)/sizeof(cbuffer[0]);
    std::string sbuffer(cbuffer, len); //5 characters in cbuffer, 1 byte each
    

    or

    int len = sizeof(cbuffer)/sizeof(cbuffer[0]);
    std::cout.write(cbuffer, len); //5 characters in buffer, 1 byte each
    

    However, you have to be careful with sizeof(cbuffer). If cbuffer is a char* (pointer) instead of a char(&)[] (array), then sizeof(ptr) will return the wrong value, and there is no way to get the correct length at that point, if the string is not null-terminated.

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

Sidebar

Related Questions

How would I manually concatenate two char arrays without using the strncpy function? Can
It can be simple but I m new about c++ In char arrays we
I wrote this function but can't get on the problem that gives me 'segmentation
I am trying to write a program that has a vector of char arrays
I'm working on a homework assignment in which I'm required to use char arrays
When should I use std::string and when should I use char* to manage arrays
I have a char array buffer that I am using to store characters that
I need to convert a char array into int and float using C The
i got a question about unsigned char array. How can i store an integer
If I have two char arrays like so: char one[200]; char two[200]; And I

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.