I need to copy a string which may contain NULL characters in between. I may run a loop but I just wanted to know if there is any other way to do this. Normal function like strcpy and all will stop reading after it encounters a NULL since it assumes end of string when it reads NULL.
The strncpy function copies the initial count characters of strSource to strDest and returns strDest. If count is less than or equal to the length of strSource, a null character is not appended automatically to the copied string. If count is greater than the length of strSource, the destination string is padded with null characters up to length count.
Hi, may i please request you to just try this code and comment on why memcpy is failing? Is it the way it goes or am I wrong somewhere? Can you suggest some workaround? Thanks for your time.
Sure! Please look up memcpy in MSDN. There is a working demo there.
See you were saying that szSource contains NULL characters right? So strlen won't work because it will return when it finds the first null character. Same with printf and other functions. They all return when the first NULL character is found. So if your string is...
What are you trying to do exactly ?
Is not because memcpy sucessfully copied the source into the destination that the '\0' character will be removed. It will still be present in the new string so as soon as you try to get its length or print the string, you will only have the begining of the string. This is totally logical.
What I am doing? PIP (Peripheral Interface Programming). I have generated a sequence of binary data which will be understood by a peripheral device. For example, we issue a print command from notepad. Its not the .txt file that is sent to the printer by the OS. But a series of printer-understood data is sent. My program generates such device-understandable data which could be just dumped into the port of the device and it will print some meaningful text or act accordingly. I have this binary (kinda junk) stored in unsigned char array. So, I am trying to figure out a way to do this.
So, what is the problem ? Your data is there but if you try to display it with a function that waits for a string, you won't be able to see past the zero char. Just send your buffer and it will work. You need to remember the size of your string of course because strlen won't work.