Go Down

Topic: [solved] How can I convert specific chars of an array to a int or a byte? (Read 285 times) previous topic - next topic

Hi everyone.

I'm trying to control some lights from (at the moment) one arduino to another via i2c by sending a String like this one 01111/255255255 (the length and the position will be always the same).

The comunication works perfect and I'm able to print the array of char that I'm receiving in the serial interface of the slave but the problem is that I don't know how to convert the data(the last 3 groups of 3 numbers) to the actual value in a byte or a int.

I would like to do something like this:

Code: [Select]


char inComming[15]; //received data

String red = inComming[6]+inComming[7]+inComming[8]; // concatenate the 3 numbers in a string

byte redValue = stringTobyte(red); // convert the string to the real value on a byte

//(I know it won't be that easy)


I've been messing around with some functions like strcat or sprintf without result (probably because I'm doing something wrong).

Any help?
no pain, no gain!!

majenko

Something more like:

Code: [Select]

int red = ((inComming[6]-'0') * 100) + ((inComming[7]-'0') * 10) +(inComming[8]-'0');

no pain, no gain!!

UKHeliBob

I was going to suggest using the atoi function
Code: [Select]

String red = inComming[6]+inComming[7]+inComming[8];
int redValue = atoi(red);
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

majenko


I was going to suggest using the atoi function
Code: [Select]

String red = inComming[6]+inComming[7]+inComming[8];
int redValue = atoi(red);


Yes, but then you have a pesky String object in the way messing up your heap.

Go Up