Pages: [1]   Go Down
Author Topic: char array to int  (Read 890 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 1
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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(){}
Logged

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Have look at "atoi".

What you're doing is converting a pointer to "char" (char*) to an integer.
« Last Edit: March 03, 2010, 10:00:28 am by GrooveFlotilla » Logged

Per Arduino ad Astra

0
Offline Offline
Shannon Member
****
Karma: 215
Posts: 12443
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

[ I won't respond to messages, use the forum please ]

Pages: [1]   Go Up
Jump to: