Archived

This topic is now archived and is closed to further replies.

Ichabod

NULL

Recommended Posts

Hi all Perhaps this issue has been discussed before but since the search function on this board does not work I had to post Well, my question is about the NULL character (ascii 0) and how it is sent over different connections. I have written programs using both Winsock and IrDA and this problem always occurs. Usually you send bytes over connections and in most cases I have seen people translate what they are sending/receiving into character strings, like send(s,"Hello world",11) and recv(s,buffer,11) where buffer is a char buffer[11]. Lets say that you need to pass some integers over for example the internet using winsock, if you know that these values never exceed 200 you could just cast them to chars and drop them on the link. But the problem seems to be the value 0, this is tranlated to NULL using a standard type cast. And on the receiving side this means the string will end on this byte if I should try standard str management functions, like strcpy, strcat and so on. In my latest project we use a coding with 2 bytes (characters) to represent a numerical value (integers), 10 bits of the total 16 bits are used to the absolute value, 1 bit for the sign and the remaining 5 for various other stuff. Is the solution to this problem not to use strxxx functions but memxxx functions instead or to use a coding scheme where the 0x00 never occurs? Or perhaps I got this all wrong, if so, please explain.. Thanks

Share this post


Link to post
Share on other sites
Just use the mem* family of functions. Not only do they handle null bytes properly they also are slightly more efficient than the str* family of functions. However, if your protocol depends at all on null terminated strings you might need to adjust things so that it sends buffer lengths.

Share this post


Link to post
Share on other sites