How to copy string with NULL
-
strncpy
will not work: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**.
Cédric Moonen Software developer
Charting controlCedric Moonen wrote:
strncpy will not work:
Yeah it won't. Was just a blind hit. Was not sure about it. Read the docs now. Sorry for that. :)
Nibu thomas A Developer Programming tips[^] My site[^]
-
Hi all, 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. NOTE: I am using plain C. :rose:
Aljechin wrote:
I need to copy a string which may contain NULL characters in between
My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.
-
Aljechin wrote:
NOTE: I am using plain C.
strncpy
ormemcpy
should help.
Nibu thomas A Developer Programming tips[^] My site[^]
char szSource[23] = {"Before\0After"}; char szDest[23]; ptr = memcpy(szDest,szSource,22); printf("%s %d",szDest,strlen(szDest));
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. :rose: -
Aljechin wrote:
I need to copy a string which may contain NULL characters in between
My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.
char szSource[23] = {"Before\0After"}; char szDest[23]; ptr = memcpy(szDest,szSource,22); printf("%s %d",szDest,strlen(szDest));
I went through that page, but not getting it. Can you show me a small code snippet of how do i copy that szSource into szDest? Sorry if a very elementary question. :rose: -
char szSource[23] = {"Before\0After"}; char szDest[23]; ptr = memcpy(szDest,szSource,22); printf("%s %d",szDest,strlen(szDest));
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. :rose:Aljechin wrote:
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 thatszSource
containsNULL
characters right? Sostrlen
won't work because it will return when it finds the first null character. Same withprintf
and other functions. They all return when the firstNULL
character is found. So if your string is..."Nibu\0is a\0\0\0good\0boy\0".
strlen
will return4
.printf
will print onlyNibu
Nibu thomas A Developer Programming tips[^] My site[^]
-
char szSource[23] = {"Before\0After"}; char szDest[23]; ptr = memcpy(szDest,szSource,22); printf("%s %d",szDest,strlen(szDest));
I went through that page, but not getting it. Can you show me a small code snippet of how do i copy that szSource into szDest? Sorry if a very elementary question. :rose: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.
Cédric Moonen Software developer
Charting control -
Aljechin wrote:
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 thatszSource
containsNULL
characters right? Sostrlen
won't work because it will return when it finds the first null character. Same withprintf
and other functions. They all return when the firstNULL
character is found. So if your string is..."Nibu\0is a\0\0\0good\0boy\0".
strlen
will return4
.printf
will print onlyNibu
Nibu thomas A Developer Programming tips[^] My site[^]
Thank you so much. I will surely check that example and get back to you with the problem status. peace homie :rose:
-
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.
Cédric Moonen Software developer
Charting controlWhat 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. :) :rose:
-
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. :) :rose:
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.
Cédric Moonen Software developer
Charting control -
char szSource[23] = {"Before\0After"}; char szDest[23]; ptr = memcpy(szDest,szSource,22); printf("%s %d",szDest,strlen(szDest));
I went through that page, but not getting it. Can you show me a small code snippet of how do i copy that szSource into szDest? Sorry if a very elementary question. :rose:Aljechin wrote:
char szSource[23] = {"Before\0After"}; char szDest[23]; ptr = memcpy(szDest,szSource,22); printf("%s %d",szDest,strlen(szDest));
char szSource[23] = {"Before\0After"}; char szDest[23]; std::copy(szSource, szSource + 23, szDest);
However, as Cedric already noted, don't expect that printf and strlen show the whole thing if you have embedded zeroes.
My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.
-
char szSource[23] = {"Before\0After"}; char szDest[23]; ptr = memcpy(szDest,szSource,22); printf("%s %d",szDest,strlen(szDest));
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. :rose:Aljechin wrote:
ptr = memcpy(szDest,szSource,22);
Use 12 instead.
Aljechin wrote:
printf("%s %d",szDest,strlen(szDest));
This is highly dependent on
szDest
being nul-terminated. Usingmemcpy()
implies that you are not treating it as a nul-terminated string.
"The largest fire starts but with the smallest spark." - David Crow
-
Aljechin wrote:
ptr = memcpy(szDest,szSource,22);
Use 12 instead.
Aljechin wrote:
printf("%s %d",szDest,strlen(szDest));
This is highly dependent on
szDest
being nul-terminated. Usingmemcpy()
implies that you are not treating it as a nul-terminated string.
"The largest fire starts but with the smallest spark." - David Crow
Thank you so much. :rose:
-
Aljechin wrote:
char szSource[23] = {"Before\0After"}; char szDest[23]; ptr = memcpy(szDest,szSource,22); printf("%s %d",szDest,strlen(szDest));
char szSource[23] = {"Before\0After"}; char szDest[23]; std::copy(szSource, szSource + 23, szDest);
However, as Cedric already noted, don't expect that printf and strlen show the whole thing if you have embedded zeroes.
My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.
Thank you so much. I will try this. :rose:
-
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.
Cédric Moonen Software developer
Charting controlThank you very much