Go Down

Topic: char array to int (Read 961 times) previous topic - next topic

Adam M

Hi I have a project that receives data from a wishield. Most is working but I have a problem converting the char array to an integer value.

I think this is a C related issue and me not properly understanding the  int() conversion method.

Any help is much appreciated

The code below illustrates the problem.

Code: [Select]

void setup(){
Serial.begin(57600);
char data[] = "128";
int myint = int(data);
Serial.println(data); // print "128"
Serial.println(myint); // print 128
Serial.println(myint++); // print 129

/*
Results in serial monitor...
128
2290
2290
*/

}

void loop(){}

Groove

#1
Mar 03, 2010, 03:58 pm Last Edit: Mar 03, 2010, 04:00 pm by GrooveFlotilla Reason: 1
Have look at "atoi".

What you're doing is converting a pointer to "char" (char*) to an integer.
Per Arduino ad Astra

MarkT

Technically its a cast, not a conversion - the value of the pointer is being re-interpreted as if it was declared int, the value is not touched.  Casting types like this is often confusing and leads to problems - and certainly casting pointers to and from integers is unlikely to be useful in an Arduino sketch.

atoi() etc are the way to go
[ I won't respond to messages, use the forum please ]

Go Up