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

  • Home
  • SEARCH
  • 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 229599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:46:47+00:00 2026-05-11T19:46:47+00:00

I am relatively new to C++. Recent assignments have required that I convert a

  • 0

I am relatively new to C++. Recent assignments have required that I convert a multitude of char buffers (from structures/sockets, etc.) to strings. I have been using variations on the following but they seem awkward. Is there a better way to do this kind of thing?

#include <iostream>
#include <string>

using std::string;
using std::cout;
using std::endl;


char* bufferToCString(char *buff, int buffSize, char *str)
{
    memset(str, '\0', buffSize + 1);
    return(strncpy(str, buff, buffSize));
}


string& bufferToString(char* buffer, int bufflen, string& str)
{
    char temp[bufflen];

    memset(temp, '\0', bufflen + 1);
    strncpy(temp, buffer, bufflen);

    return(str.assign(temp));
}



int main(int argc, char *argv[])
{
   char buff[4] = {'a', 'b', 'c', 'd'};

   char str[5];
   string str2;

   cout << bufferToCString(buff, sizeof(buff), str) << endl;

   cout << bufferToString(buff, sizeof(buff), str2) << endl;

}
  • 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-11T19:46:47+00:00Added an answer on May 11, 2026 at 7:46 pm

    Given your input strings are not null terminated, you shouldn’t use str… functions. You also can’t use the popularly used std::string constructors. However, you can use this constructor:

    std::string str(buffer, buflen): it takes a char* and a length. (actually const char* and length)

    I would avoid the C string version. This would give:

    std::string bufferToString(char* buffer, int bufflen)
    {
        std::string ret(buffer, bufflen);
    
        return ret;
    }
    

    If you really must use the C-string version, either drop a 0 at the bufflen position (if you can) or create a buffer of bufflen+1, then memcpy the buffer into it, and drop a 0 at the end (bufflen position).

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

Sidebar

Related Questions

I am relatively new to nodejs etc. Anyway I have a program that I
I am relatively new to Android development. My requirement is such that I have
I am relatively new to Xcode and one thing that has bothered me is
Note: I'm relatively new to Objective-C and am coming from Java and PHP. Could
Relatively new to JQuery. I've got some code that does a banner swap with
Im relatively new to PHP but have realized it is a powerfull tool. So
I am relatively new to django. I have defined my db schema and validated
Relatively new to python. I recently posted a question in regards to validating that
Relatively new to mySQL so this is probably an easy one: I have a
I'm relatively new to programming and a recent project I've started to work on

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.