how to convert string to negative value?

Dear All,

Please let me know how to convert String to negative value? let say "-4000" converted to - 4000. I got frustrated about this.. Thank you..

Best regards, Harooney

Please let me know how to convert String to negative value? let say “-4000” converted to - 4000.

The underlying assumption here is that toInt() doesn’t deal correctly with negative numbers. I do not accept that that assumption is true.

Even if it were, it isn’t rocket science to discover that indexOf(’-’) is non-negative, and negate the value that toInt() returned.

You need to post your code and proof.

Do you mean that you want to convert the number -4000 into +4000?

OR do you mean that you have 5 characters - 4 0 0 0 and you want to remove the first character?

It would probably help if you give an overview of what you are trying to achieve.

...R

String to Int Functions

you can use map ,,

int valin ; in valout ; valout= map(valin ,0,1000,-1000,1000); means 0 wil to 1000 comes in ,, and wil be changed ,, let say 0 = -1000 and 1000 wil be 1000 + :D

Thanks for your quick replies. Value that will be converted is too variate. I get a string from a device. The string can be in "positive" or "negative" format. Let say " 4000" or "- 4000". I'm using this function (I get this from google's searching result).

float GetValue(String pString){ char vTempValue[10]; pString.toCharArray(vTempValue,sizeof(vTempValue)); return atof(vTempValue); }

When I receive "- XXXX" string, I always got 0 from GetValue function.

Please let me know how to fix this.

Thanks

PaulS, I need to convert a string to a value. This value can be a negative value. example "- 4000" converted to "4000", "0" to 0, "1000" to 1000. I used atof() but It's not working. Thanks

i think it because of the float ,, it also leave fractions ,, like 0.0 i prefer map ,, have to see the sketch ,,

use below code as a reference.

#include<stdio.h>
#include<string.h>
int main()
{
	char* str = "-bijendra";
	char *destination;
	char* sign = "-";
	char* tok;
	tok = strtok(str, sign);
	while(tok != NULL)
	{
         	destination = tok;
		tok = strtok(NULL, sign);
	}
	printf("%s", destination);
	return 0;
}

use below code as a reference.

For what? It has NOTHING to do with OP's problem.

float  GetValue(String pString){
  char vTempValue[10];
  pString.toCharArray(vTempValue,sizeof(vTempValue));
  return  atof(vTempValue);
}

Show the real code. Add Serial.print() statements to print the String pString. Add Serial.print() statements to print vTempValue. Add Serial.print() statements to print the returned value.

float  GetValue(String pString)
{
  Serial.print("pString: [");
  Serial.print(pString);
  Serial.println("]");
  
  char vTempValue[10];
  pString.toCharArray(vTempValue,sizeof(vTempValue));
  Serial.print("vTempValue: [");
  Serial.print(vTempValue);
  Serial.println("]");
  
  return  atof(vTempValue);
}

void setup()
{
  Serial.begin(115200);
  
  String pos = "4000";
  String neg = "-4000";
  
  float p = GetValue(pos);
  Serial.print("p: ");
  Serial.println(p);
  
  float n = GetValue(neg);
  Serial.print("n: ");
  Serial.println(n);
}

void loop()
{
}

produces:

pString: [4000] vTempValue: [4000] p: 4000.00 pString: [-4000] vTempValue: [-4000] n: -4000.00