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
}
In your code, the string terminator is not added. You also skip one character between the copies (since you have
max_lenas17while you only copy16characters).I would propose a solution using the standard library instead: