Find Substring In Array Of Chars, Parse It And Save To New Char Array
Maybe your like
Hello, I have array of chars and I need parse this array. Array start and ends with unimportant characters. I need find "btn1=1" or "btn1=0" and save value to boolean btn1 and so on... Sha1 is always 40 chars long. At the end I need copy all important chars (bold text) to my new array allParams. Input is always same. Only number is different (btn1=1"/"btn1=0") and sha hash is different. I try:
#include <string.h> void setup() { Serial.begin(9600);
char input[] = {"some unimportant \n databtn1=1&btn2=0&sha1=a5b5da1c91aa2ea0c7e630db6c8e3175058e907f some\t unimportant \ndata"}; boolean btn1; boolean btn2; char sha1[40]; char allParams[100];
char tmp[6]; if ( strncpy(tmp, strstr(input,"btn1=1"), 6 ) ) btn1 = true; if ( strncpy(tmp, strstr(input,"btn1=0"), 6 ) ) btn1 = false; Serial.println(btn1); } void loop() {}
but it doesn't work. Can you help me, please?
martin159 September 1, 2013, 10:54pm 2I write working code for parse btn1 and btn2:
#include <string.h> void setup() { Serial.begin(9600);
char input[] = {"some unimportant \n data btn1=0&btn2=1&sha1=a5b5da1c91aa2ea0c7e630db6c8e3175058e907f some \nunimportant \ndata"}; boolean btn1; boolean btn2; char sha1[40]; char allParams[100]; memset(allParams, 0, 100); strcat(allParams, "?");
if ( strstr(input, "btn1=0") ) { strcat(allParams, "btn1=0&"); btn1 = false; } if ( strstr(input, "btn1=1") ) { strcat(allParams, "btn1=1&"); btn1 = true; }
if ( strstr(input, "btn2=0") ) { strcat(allParams, "btn2=0&"); btn2 = false; } if ( strstr(input, "btn2=1") ) { strcat(allParams, "btn2=1&"); btn2 = true; }
Serial.println(btn1); Serial.println(btn2); Serial.println(allParams);
} void loop() {}
Output:
0 1 ?btn1=0&btn2=1&but I still can't parse sha1 hash. How I can use regular expressions in Arduino? Is regex (sha1=)\w{40} good?
tmd3 September 1, 2013, 10:57pm 3martin159: ... but it doesn't work. Can you help me, please?
Here's what I think you expected: this sketch would print the character, "1". Here's what I think you got: the sketch prints "0". A "0" seems to indicate that the text "btn1=1" wasn't found in the string, when in fact that text is in the string.
Here's what I think is wrong: This code -
if ( strncpy(tmp, strstr(input,"btn1=1"), 6 ) )tests the return value of strncpy(). That return value will always be tmp, a pointer a character array: strncpy() returns it's destination argument. That won't be the null pointer, so the test will always evaluate as true.
Here's what I think you want to do:
if ( strstr(input,"btn1=1") ) {If strstr() finds the text in the input string, it will return a pointer to the first character of that text; that will evaluate as true. If it doesn't, it will return the null pointer, which will evaluate as false.
If you want to store the string in the array tmp[], you'll need to execute strncpy() to capture it. You'll also need to make it's size 7 instead of 6, to make room for the terminating null character, and you'll have to add that null character yourself, because strncpy doesn't do it. There's not much point in keeping it, though - if it contains anything, you already know exactly what it contains.
For your conveninece, here's a link to instructions about posting to this forum: Read this before posting a programming question ... - Programming Questions - Arduino Forum. Relevant issues are covered in item 6.
tmd3 September 1, 2013, 11:12pm 4martin159: ... but I still can't parse sha1 hash.
I don't see anything in the code that looks for "sha1".
How I can use regular expressions in Arduino?
I don't know of a way to use regular expressions on the Arduino. Someone may have written a limited-function library; I'll leave that search to you and Google.
Is regex (sha1=)\w{40} good?
How did it work when you tried it?
I think that your best bet would be to look for "sha1=", just like you look for button identifiers, and then capture those characters and the next 40 characters. You can trust that the characters are correct, or you can test the characters to make sure that they're valid hex.
martin159 September 1, 2013, 11:34pm 5OK, I do it without regex. I find copy function.
#include <string.h> #include <string> void setup() { Serial.begin(9600); char input[] = {"some unimportant \n data btn1=0&btn2=1&sha1=a5b5da1c91aa2ea0c7e630db6c8e3175058e907f some \nunimportant \ndata"}; boolean btn1; boolean btn2; char sha1[40]; char allParams[100]; memset(allParams, 0, 100); strcat(allParams, "?"); if ( strstr(input, "btn1=0") ) { strcat(allParams, "btn1=0&"); btn1 = false; } if ( strstr(input, "btn1=1") ) { strcat(allParams, "btn1=1&"); btn1 = true; } if ( strstr(input, "btn2=0") ) { strcat(allParams, "btn2=0&"); btn2 = false; } if ( strstr(input, "btn2=1") ) { strcat(allParams, "btn2=1&"); btn2 = true; } input.copy(sha1, 40, (strstr(input, "sha1=")+5) ); Serial.println(btn1); Serial.println(btn2); Serial.println(sha1); Serial.println(allParams); } void loop() {}I get error:
TextFind.ino: In function ‘void setup()’: TextFind:34: error: request for member ‘copy’ in ‘input’, which is of non-class type ‘char [108]’ martin159 September 1, 2013, 11:52pm 6Now it works fine. Working code:
#include <string.h> void setup() { Serial.begin(9600);
char input[] = {"some unimportant \n data btn1=0&btn2=1&sha1=a5b5da1c91aa2ea0c7e630db6c8e3175058e907f some \nunimportant \ndata"}; char sha1[41]; memset(sha1, 0, 41); memcpy(sha1, (strstr(input, "sha1=")+5), (sizeof(char)*40) ); Serial.println(sha1);
} void loop() {}
Related topics
| Topic | Replies | Views | Activity |
|---|---|---|---|
| Trouble finding a substring in a char array. Reading 2 UART Streams. Programming | 7 | 1768 | May 6, 2021 |
| substring from a char array Programming | 29 | 22466 | May 5, 2021 |
| New regular expression library released Programming | 45 | 43414 | May 5, 2021 |
| Prendere una substring da un char array Software | 6 | 3179 | May 7, 2021 |
| Usare solo alcuni caratteri di una stringa Software | 9 | 1187 | May 7, 2021 |
Tag » Arduino Find Text In Char Array
-
Find String In Char Array - Programming Questions - Arduino Forum
-
Searching String In Char Array - Arduino Forum
-
Finding A Specific Char In Array - Arduino Forum
-
Trouble Finding A Substring In A Char Array. Reading 2 UART Streams.
-
IndexOf() - Arduino Reference
-
Substring From A Char Array - Programming Questions - Arduino Forum
-
How To Check If String Value Is In Array - Arduino Forum
-
Get Words (actual Words) From A Char Array And See If It's In Another ...
-
Arduino String To Char Array Code Example - Grepper
-
Summary Of Strings With Arduino - AranaCorp
-
Arduino C++, Unable To Convert String To Char* - Stack Overflow
-
2.6 Understanding Character Strings In Arduino - ArduinoPlatform
-
Arduino String Functions - 免费编程教程
-
How To Get Char Array's Length - Arduino Stack Exchange