Trying to figure out why the function doesn't pass back a value to the main loop. I have read and read, and tried. The value gets passed to the function but doesn't appear to get passed back.
I put it into a simple loop to try to figure out why it's not working. I am trying to get the %xx hex code that gets sent from a web page turned back into the character that was sent. The code may be goofy but I am still in the learning stages.
Obviously being a bonehead. Can anyone shed some light as to what I'm missing?
String newval;
String req="/a?usrpass=tomatosoup%21&ename=banana%21&empass=flipflop%3D";
void setup()
{
Serial.begin(115200);
}
void loop()
{
Serial.print(" req =");
Serial.println(req);
newval = req.substring(req.indexOf('=') + 1, req.indexOf('&')); //get input
Serial.print(" newval ="); Serial.println(newval);
String new2;
int l;
new2 = changechar(newval);
Serial.print("new2 = "); Serial.println(new2);
newval=new2;
if (newval == "&")newval = ""; //.................No value=""
l = newval.length(); //check length
if (l == 0) l = 1; //too short so mqake l=1
Serial.print("newval = "); Serial.println(newval);
req = req.substring(req.indexOf('&') + 1);
}
String changechar(String newval1) //......................................Change character
{
//stuff to remove the digits from req every time
//String schar;
if (newval1.indexOf('%') > 0) {
Serial.print("newval = "); Serial.println(newval1);
Serial.print("Location of % = "); Serial.println(newval1.indexOf('%'));
String schar = newval1.substring(newval1.indexOf('%') + 1, newval1.indexOf('%') + 3);
String nschar;
long A = strtol(schar.c_str(), NULL, 16);
nschar = (char) A;
Serial.println();
Serial.print("Schar="); Serial.println(schar);
Serial.print(" nschar= "); Serial.println(nschar);
String newreq = "";
newreq = newval1.substring(0, newval1.indexOf('%') );
newreq += nschar;
newreq += newval1.substring(newval1.indexOf('%') + 3);
Serial.print("newchar= "); Serial.println(newreq);
newreq = newreq;
}
return newval1;
}
The value gets passed to the function but doesn't appear to get passed back.
Could you be a little less vague? What value gets passed to what function?
It appears that you are passing a String to changechar(), and printing the return value from the function, successfully. So, it is difficult to understand your issue.
All of my code that calls functions includes two statements in the function:
String StringToPassToFunction;
String requestFromWeb="/a?usrpass=tomatosoup%21&ename=banana%21&empass=flipflop%3D";
void setup()
{
Serial.begin(115200);
}
void loop()
{
Serial.print(" requestFromWeb =");
Serial.println(requestFromWeb);
StringToPassToFunction = requestFromWeb.substring(requestFromWeb.indexOf('=') + 1, requestFromWeb.indexOf('&')); //get input
Serial.print(" StringToPassToFunction ="); Serial.println(StringToPassToFunction);
String NewValueReturnedfromFunction;
NewValueReturnedfromFunction = changechar(StringToPassToFunction);
Serial.print("NewValueReturnedfromFunction = "); Serial.println(NewValueReturnedfromFunction);
requestFromWeb = requestFromWeb.substring(requestFromWeb.indexOf('&') + 1);
}
String changechar(String StringFromMainLoop) //......................................Change character
{
//stuff to remove the digits from req every time
//String specialCharacter;
if (StringFromMainLoop.indexOf('%') > 0) {
Serial.print("StringFromMainLoop = "); Serial.println(StringFromMainLoop);
Serial.print("Location of % = "); Serial.println(StringFromMainLoop.indexOf('%'));
String specialCharacter = StringFromMainLoop.substring(StringFromMainLoop.indexOf('%') + 1, StringFromMainLoop.indexOf('%') + 3);
String newSpecialCharacter;
long A = strtol(specialCharacter.c_str(), NULL, 16); //Convert to a long integer
newSpecialCharacter = (char) A; //change integer to a character
String newStringToPassBack = "";
newStringToPassBack = StringFromMainLoop.substring(0, StringFromMainLoop.indexOf('%') ); //get part of string before %.
newStringToPassBack += newSpecialCharacter; //Add the new character ! or whatever
newStringToPassBack += StringFromMainLoop.substring(StringFromMainLoop.indexOf('%') + 3); //add any thing behind the %21 or whatever special chacter
Serial.print("newStringToPassBack= "); Serial.println(newStringToPassBack);
StringFromMainLoop = newStringToPassBack; //put the whole string back into the string from the main loop with the ! in it
}
return StringFromMainLoop;
}
Why do you want to trash the original string? If that is really what you want to do, why does the function need to return anything? Back in loop, NewValueReturnedfromFunction and StringToPassToFunction will contain the same thing.
You can take meaningful names to extremes. 8)
Does returning the proper value from the function correct your original problem?
I understand that %21 and ! are the same thing. 33 decimal, %21 Hex, Octal 041, web ! are all Character ! according to the ascii table I'm looking at.
When web page value is submitted by a get statement, instead of getting ! it sends a %21. I want to convert that hex number to it's character equivalent so I can use it to populate a web field on a web page, or write it to an eeprom as part of a password for example, If I send %21 to a web page it'll show in the field as %21 not !. If I write to an eeprom it'll use 3 bytes as %21 and 1 byte as !.
My problem is understanding why newStringToPassBack shows as a ! but when I put that string back into StringFromMainLoop (StringFromMainLoop=newStringToPassBack;) and return StringFromMainLoop it shows as %21. It's like it hasn't changed. So am I passing the string back correctly?
It looks like I've solved it. I am not sure what I did but it now works. I must have changed something small and insignificant, but that's all it takes to go from hero to zero!
Here's the fixed code. I am going to work on it to reduce the number of variables.
String StringToPassToFunction;
String requestFromWeb="/a?usrpass=tomatosoup%21&ename=banana%21&empass=flipflop%3D";
void setup()
{
Serial.begin(115200);
}
void loop()
{
Serial.print(" requestFromWeb =");
Serial.println(requestFromWeb);
StringToPassToFunction = requestFromWeb.substring(requestFromWeb.indexOf('=') + 1, requestFromWeb.indexOf('&')); //get input
Serial.print(" StringToPassToFunction ="); Serial.println(StringToPassToFunction);
String NewValueReturnedfromFunction;
NewValueReturnedfromFunction = changechar(StringToPassToFunction);
Serial.print("NewValueReturnedfromFunction = "); Serial.println(NewValueReturnedfromFunction);
requestFromWeb = requestFromWeb.substring(requestFromWeb.indexOf('&') + 1);
}
String changechar(String StringFromMainLoop) //......................................Change character
{
//stuff to remove the digits from req every time
//String specialCharacter;
if (StringFromMainLoop.indexOf('%') > 0) {
Serial.print("StringFromMainLoop = "); Serial.println(StringFromMainLoop);
Serial.print("Location of % = "); Serial.println(StringFromMainLoop.indexOf('%'));
String specialCharacter = StringFromMainLoop.substring(StringFromMainLoop.indexOf('%') + 1, StringFromMainLoop.indexOf('%') + 3);
String newSpecialCharacter= "";
long A = strtol(specialCharacter.c_str(), NULL, 16); //Convert to a long integer
newSpecialCharacter = (char) A; //change integer to a character
String newStringToPassBack;
newStringToPassBack = StringFromMainLoop.substring(0, StringFromMainLoop.indexOf('%') ); //get part of string before %.
newStringToPassBack += newSpecialCharacter; //Add the new character ! or whatever
newStringToPassBack += StringFromMainLoop.substring(StringFromMainLoop.indexOf('%') + 3); //add any thing behind the %21 or whatever special chacter
Serial.print("newStringToPassBack= "); Serial.println(newStringToPassBack);
StringFromMainLoop = newStringToPassBack; //put the whole string back into the string from the main loop with the ! in it
}
return StringFromMainLoop;
}