Wednesday, 2 October 2013

Truncation of string during passing to function

Truncation of string during passing to function

Given
struct sendCommand {
char stringBytes[INPUT_BUF_SIZE]; // INPUT_BUF_SIZE: 32k
};
printf("%s\n", (ptr->stringBytes)); // shows full string (correct):
11223344556677aabbcc<b>00223344556677aabbccddee</b>
but when calling
hexStringToHexBytesArray ((ptr->stringBytes));
Within the function (it shows on part of the string starting from the
middle). Why is this so?
// convert hexdecimal string to int array (hexadecimal values) and placed
in allocated buffer starting from the buffer index 0
void hexStringToHexBytesArray (char* hexStringBytes) {
//char* hexByteInStr = "";
char byteInStr[3] = ""; // XX\0
//char byteInStr[5] = "0x"; // 0xXX\0
int numberOfBytes = (strlen(hexStringBytes)/2);
printf("%s\n", (hexStringBytes)); // shows 00223344556677aabbccddee
int i = 0;
for(i = 0; i < numberOfBytes; i++){
strncat(byteInStr, &hexStringBytes[(i*2)], 1); //
insert 1st hex character
strncat(byteInStr, &hexStringBytes[(i*2)+1], 1); // insert
2nd hex character
//byteInStr[4] = '\0';
byteInStr[2] = NULL; \\ '\0';
genericOutputBuffer[i] = (uint8)strtol(byteInStr, NULL, 16);
// without "0x" header, last paramater is 16
//strcpy(byteInStr, "0x");
strcpy(byteInStr, "");
}
}

No comments:

Post a Comment