unsigned char Response[269]={0x00};
This initializes Response
to a sequence of 269 identical values, each of which is zero. Note that 0x00
, 0
, and '\0'
are all the same thing.
strlen(Response)
This is actually invalid, though some compilers may permit it. strlen
requires an argument of type char*
; Response
is an array of unsigned char
, which is converted to a pointer of type unsigned char*
. The types are not compatible, and there is no implicit conversion. You should have gotten a compile-time warning (if you did, please include the warning in your question).
If we change the definition of Response
to:
char Response[269] = {0x00};
then the strlen
call becomes valid -- and will return 0
. strlen
scans for the first '\0'
null character -- and you've filled Response
with null characters.
At the top of your question, you say you have a string:
56 69 56 4F 74 65 63 68 32 00 2C 00 00 27 6F 23 84 0E 32 50 41 59 2E 53 59 53 2E 44 44 46 30 31 A5 11 BF 0C 0E 61 0C 4F 07 A0 00 00 00 04 10 10 87 01 01
That doesn't have anything to do with the code you showed us. It's also ambiguous; is it a string of hex digits and spaces: "56 69 56 ..."
, or is it something like "\x56\x69\x56..."
? If the 56 69
sequence is intended to represent the contents of your array, please update your question to show the actual code that creates it.
Finally:
for(i=0;i<=strlen(Response);i++)
sprintf(Response_PPSE+(2*i),"%02X",Response[i]);
Calling strlen
on each iteration of the loop can be quite inefficient. strlen
has to scan the entire string each time to determine its length. Once you get the other problems straightened out, you should call strlen
once before the loop and use the stored value.
However, you probably don't want to be using strlen
in the first place. strlen
is applied to strings. A "string" is a sequence of characters terminated by a null character (0
, '\0'
, 0x00
). You appear to have a sequence of unsigned characters in which 0x00
is a valid value, not a terminator. Rather than using strlen
to compute the length of the array, you need to keep track of the length by some other means.