strcat_memcpy
-
I need to send the data to the server in a manner like 00 12 34 00 00 54 when I am doing a strcat of 00 to 1243 or strcat of 1234 to 000054 it's not concatenating 00 to string and the result is 1234 only. I know strcat treats 00 as a terminating null character that's why it is considering 00 as terminator.I have tried with the memcpy also but then in the server side 00 is coming as 3030. How can I make my program to treat 00 as the part of the array not as the terminating null character or to treat 00 as it is,not the ascii value as 30.
-
I need to send the data to the server in a manner like 00 12 34 00 00 54 when I am doing a strcat of 00 to 1243 or strcat of 1234 to 000054 it's not concatenating 00 to string and the result is 1234 only. I know strcat treats 00 as a terminating null character that's why it is considering 00 as terminator.I have tried with the memcpy also but then in the server side 00 is coming as 3030. How can I make my program to treat 00 as the part of the array not as the terminating null character or to treat 00 as it is,not the ascii value as 30.
If you are sending pure binary data then forget about functions that treat byte sequences as zero terminated strings. Don't use strcat but memcpy should be okay to move bytes here and there if you use it correctly. Along with your binary data you should store the length/size of the data in an int or size_t variable. If you are using C++ you could simply use an std::vector for this job.
class CStorage
{
size_t m_Size;
char m_Bytes[100];public:
CStorage()
: m_Size(0)
{}
void Append(const char* data, size_t size)
{
memcpy(m_Bytes+m_Size, data, size);
}
};OR
#include <vector>
#include <iterator>static const char appendable[] = "abcdefgh";
std::vector<char> bytes;
std::copy(appendable, appendable+sizeof(appendable)/sizeof(appendable[0]), std::back_inserter(bytes)); -
I need to send the data to the server in a manner like 00 12 34 00 00 54 when I am doing a strcat of 00 to 1243 or strcat of 1234 to 000054 it's not concatenating 00 to string and the result is 1234 only. I know strcat treats 00 as a terminating null character that's why it is considering 00 as terminator.I have tried with the memcpy also but then in the server side 00 is coming as 3030. How can I make my program to treat 00 as the part of the array not as the terminating null character or to treat 00 as it is,not the ascii value as 30.
I explained yesterday[^] that you cannot use the string functions for binary data that contains null characters: you must use
memcpy()
. If the server is receiving3030
rather than00
, then it means your sending code is not converting it from characters to binary.Use the best guess
-
If you are sending pure binary data then forget about functions that treat byte sequences as zero terminated strings. Don't use strcat but memcpy should be okay to move bytes here and there if you use it correctly. Along with your binary data you should store the length/size of the data in an int or size_t variable. If you are using C++ you could simply use an std::vector for this job.
class CStorage
{
size_t m_Size;
char m_Bytes[100];public:
CStorage()
: m_Size(0)
{}
void Append(const char* data, size_t size)
{
memcpy(m_Bytes+m_Size, data, size);
}
};OR
#include <vector>
#include <iterator>static const char appendable[] = "abcdefgh";
std::vector<char> bytes;
std::copy(appendable, appendable+sizeof(appendable)/sizeof(appendable[0]), std::back_inserter(bytes)); -
Of course. :-)