Can anyone help me with this problem that has me pulling out my hair!!
Problem #1 :
How can I declare a char* to contain ""(quotation marks)/curly brackets/whatever special characters I want AS IT IS.To make things easier to understand, I want to do the following:
char* TestString="{"fname":"John","lname":"Doe","age":25}"
That is I want TestString to contain : {"fname":"John","lname":"Doe","age":25} as it is, the quotation marks should become a part of the string.
Obviously this is not possible with the statement I have written above as the string gets terminated as soon as it encounters the " which I want to be a part of the string.
Problem 2:
I have a char* which contains the following : which i want to convert to :
%7B%22John+Doe%22%7D {"John Doe"}
%22John+Doe%22 "John Doe"
John+Doe John Doe
i.e. all HEX to be converted to ASCII.Is there a simple way to do this?
As must be quite obvious by now, I am really really new to Arduino programming, sorry if this seems like a totally noob question but I searched all around for this but could not find the answer or maybe I was doing things wrong.
Thanks to anyone who answers this!
For the 2nd problem, the String class has methods to replace one string with another. You could replace all occurrences of %7B with {, all occurrences of %22 with ", and all occurrences of + with a space.
#include <Regexp.h>
void setup ()
{
Serial.begin (115200);
// what we are searching
char buf [100] = "%7B%22John+Doe%22%7D";
// for matching regular expressions
MatchState ms;
// set address of string to be searched
ms.Target (buf);
// easy part, replace + by space
for (unsigned int index = 0;
ms.Match ("%+", index) > 0;
index = ms.MatchStart + ms.MatchLength)
buf [ms.MatchStart] = ' ';
// look for %xx (have to use %% to match %), in other words % followed by 2 hex digits
for (unsigned int index = 0;
ms.Match ("%%(%x%x)", index) > 0;
index = ms.MatchStart + 1) // go forward the one byte
{
char hexdigits [3];
// get first capture
ms.GetCapture (hexdigits, 0);
// convert from hex to printable
byte c = strtol (hexdigits, NULL, 16);
// copy the rest of the buffer backwards/forwards to allow for the length difference
// the +1 is to copy the null terminator
memmove (&buf [ms.MatchStart + 1], // step past the replacement
&buf [ms.MatchStart + ms.MatchLength], // from where the match ended
strlen (buf) - ms.MatchStart - ms.MatchLength + 1);
// copy in the replacement
buf [ms.MatchStart] = c;
} // end of while
Serial.println (buf);
} // end of setup
void loop () {}
This doesn't use the string library and thus does not consume any extra memory other than what is required here (ie. no memory fragmentation).
I have now uploaded an updated version of the regular expression library, which makes doing replacements much simpler. The example code to fix up the hex codes is now simpler. For each match a callback function is called which can just work out a replacement. The replacement of a "+" by a space is now just one line of code.
#include <Regexp.h>
// called for every match
void replace_callback (const char * match, // what we found
const unsigned int length, // how long it was
char * & replacement, // put replacement here
unsigned int & replacement_length, // put replacement length here
const MatchState & ms) // for looking up captures
{
static byte c; // for holding replacement byte, must be static
char hexdigits [3]; // to hold hex string
// get first capture
ms.GetCapture (hexdigits, 0);
// convert from hex to printable
c = strtol (hexdigits, NULL, 16);
// set as replacement
replacement = (char *) &c;
replacement_length = 1;
} // end of replace_callback
void setup ()
{
Serial.begin (115200);
// what we are searching
char buf [100] = "%7B%22John+Doe%22%7D";
// for matching regular expressions
MatchState ms (buf);
// easy part, replace + by space
ms.GlobalReplace ("%+", " ");
// replace %xx (eg. %22) by what the hex code represents
ms.GlobalReplace ("%%(%x%x)", replace_callback);
Serial.println (buf);
} // end of setup
void loop () {}