0

I'm writing in Keil C51 using Zigbee.

Serial_txString0("AT+UCAST:000D6F0000BB769D=StartGreen");        
tx_port0(0x0D);

For example, I will receive "AT+UCAST:000D6f0000BB769D=StartGreen", I want to filter that to only get "StartGreen".

2 Answers 2

4

How about:

char *eq = strrchr(str, '=');
if (eq)
    printf("%s\n", eq + 1);
Sign up to request clarification or add additional context in comments.

2 Comments

I am a newbieWhat does the whole thing do?
@cnicutar @JiaYuan Finds a pointer to the right most =, increments one step (to the S) and then prints it.
0

From one example, I can't really tell based on what do you want to filter. Does something like this suffice (extracting the part of string after =) ?

char* filter(char* input) {
   int i = 0;
   while (input[i] && input[i] != '=') i++;

   return &input[i + 1];
}

Note that this does not copy the string, only refers to the right part of it. If you want to actually extract the filtered part...

void filter(char* input, char* output) {
   int i = 0;
   while (input[i] && input[i] != '=') i++;

   strcpy(output, &input[i + 1]);
}

...using strcpy in <string.h>.

4 Comments

while (input[i] != '=') i++; This assumes that the string will contain a '=' character. If it doesn't, it will iterate beyond the null termination and check the whole computer's memory in search of a binary goo that is equivalent to the ASCII code '='. Use strchr() instead, or write code that checks for null termination.
@Lundin Yes, I realized that, but I assumed that he is expecting the = to be there. But ok, I changed it accordingly.
...so I must add that now, if = is not present in the string, this will return an empty string, what is probably semantically correct.
The input is obviously a serial bus, UART, SPI or similar. So none of the data can be trusted. In another situation you could make such assumptions about the data, but not here. The whole string, including the AT command, must be verified to have the correct format.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.