Char array to int

Hi!

I receive with my Arduino FIO (as a ZigBee packet ) a char array that contents an int value. For example:

I receive char message={1,2,3,4,5}. I want to transform this array to an int number, the number would 12345.

How can I to do this transformation?

Thanks!

First add add a zero-byte ('\0') at the end of your string (that's after the 5) to terminate it properly if this isn't already the case and then call either atoi or atol to convert the string to integer or long.

int i = atoi (message);
long l = atol (message);

Korman

Thanks Korman,

I´m trying to do it, but atoi allways give me a zero value. My code is the follow:

char message[10];

// now fill our zb rx class
xbee.getResponse().getZBRxResponse(rx);

nss.println(“Got an rx packet!”);

if (rx.getOption() == ZB_PACKET_ACKNOWLEDGED) {
// the sender got an ACK
nss.println(“packet acknowledged”);
}
else {
nss.println(“packet not acknowledged”);
}

nss.print("checksum is ");
nss.println(rx.getChecksum(), HEX);

nss.print("packet length is ");
nss.println(rx.getPacketLength()-6, DEC);

for (int i = 6; i <= rx.getDataLength(); i++) {
nss.print(“frame data [”);
nss.print(i, DEC);
nss.print("] is ");
char stringOne = char(rx.getData());

  • stringOne = hexNibbleToChar(stringOne);*

  • //If we are in the final character… we put in it ‘\0’*

  • if(i==rx.getDataLength()){*

  • message[i-6] = (char)’\0’;*

  • nss.print("Final ");*

  • nss.println(message[i-6], DEC);*

  • }*

  • else*

  • message[i-6] = stringOne;*

_ nss.println(rx.getData(), DEC);_
* nss.print(message[i-6], DEC);*
* }*

* nss.println(""); *
* nss.print("TimeStamp "); *
* nss.println(atoi(message)); //==> Allways returns zero :frowning: *

[/quote]
What is bad in my code???
Thanks again!!!

What is returned by this:

nss.print(">");
nss.print(message);
nss.println("<");

Please include the spaces and the ><. That will shed a some light on your problem.

Korman

With the 0021 IDE a numerical character string (readstring) can be turned into a number like below:

  if (readstring.length() >0) {
    Serial.println(readstring);  //so you can see the captured string 
    char carray[readstring.length() + 1]; //determine size of the array
    readstring.toCharArray(carray, sizeof(carray)); //put readstringinto an array
    int n = atoi(carray); //convert the array into an Integer 
    myservo.writeMicroseconds(n); // for microseconds
    //myservo.write(n); //for degees 0-180
    readstring="";
  }