Go Down

Topic: char array to int (Read 1 time) 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(){
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...


void loop(){}


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


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 will NOT respond to personal messages, I WILL delete them, use the forum please ]

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131