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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T23:29:00+00:00 2026-06-08T23:29:00+00:00

I have to copy data into sets of 16 bytes. How do i do

  • 0

I have to copy data into sets of 16 bytes. How do i do it with an easy way?
I’ve come up with the below algorithm but how do i know if the null terminator has been added? thanks! 🙂

std::string input
//get message to send to client
std::cout << "Enter message to send (type /q to quit) : ";  
getline(std::cin, input);
input += '\n';

const char *data = input.c_str();

len = strlen(data)+1;
int max_len =17;

//split msg into 16 bytes
for(int i = 0; i < len ; i+=max_len)
{
    char tempStr[max_len];
    //get next 16 bytes of msg and store
    for(int j = 0; j < max_len ; j++)
    {           
        tempStr[j] = data[j+i];
    }//end of for loop

     //Do some stuff to tempStr
}
  • 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-08T23:29:01+00:00Added an answer on June 8, 2026 at 11:29 pm

    In your code, the string terminator is not added. You also skip one character between the copies (since you have max_len as 17 while you only copy 16characters).

    I would propose a solution using the standard library instead:

    std::string::const_iterator pos = input.begin();
    
    while (pos < input.end())
    {
        // Create a temporary string containing 17 "null" characters
        char tmp[17] = {'\0' };
    
        // Make sure we co no copy beyond the end
        std::string::const_iterator end =
            (pos + 16 < input.end() ? pos + 16 : input.end());
    
        // Do the actual copying
        std::copy(pos, end, tmp);
    
        // Advance position
        pos += 16;
    
        // Use the string in 'tmp', it is properly terminated
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to copy a bunch of data from one database table into another.
I had written program which copy data into txt file program is given below
I have a script that I am using to copy data from a table
I have a file, where I want to copy some data of the file
I have two arrays. I'd like to copy ranges of data from one of
I have data in an SQL Server 2005 database which I need to copy
i want copy pdf file from application path(/data/data/package name) to sdcard.for that i have
I'm trying to take a CGImage and copy its data into a buffer for
I'm using SQLBULKCOPY to copy some data-tables into a database table, however, because the
I'm trying to copy data into a unsigned char buffer within an array of

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.