Converts an ASCIIZ (null-terminated) string to a length-preceded string that is not longer than the specified maximum length.
#include <string.h> int ASCIIZToMaxLenStr ( char *lenString, const char *ASCIIZstring, size_t maximumLength);
(OUT) Points to the destination (length-preceded) string.
(IN) Points to the source string in ASCIIZ format.
(IN) Specifies the maximum number of characters to place in the new string.
If successful, returns 0; otherwise, returns -1.
A length-preceded string has the length of the string in the first byte, followed by the characters in the string; it cannot exceed 255 characters. If the ASCIIZstring string is longer that 255 characters, this function returns -1, and lenString contains maximumLength characters. The remaining characters of the ASCIIZstring are not copied to lenString.
Because length-preceded strings only have one byte to store the size of the string, the maximum size of the string is 255. Passing a maximum size larger than 255 produces unpredictable results.
#include <string.h> #include <errno.h> main() { char srcString[256]; char destString[256]; int ccode; size_t maxSize; strcpy(srcString,"This is the message"); maxSize = 100; ccode = ASCIIZToMaxLenStr(destString, srcString, maxSize); if(ccode == ESUCCESS) printf("The string fit\n"); else printf("The string was too long to fit in the allotted space.\n"); }