Pages: [1]   Go Down
Author Topic: [solved] How can I convert specific chars of an array to a int or a byte?  (Read 237 times)
0 Members and 1 Guest are viewing this topic.
Madrid
Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

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?
« Last Edit: March 23, 2013, 11:16:09 am by Hinjeniero » Logged

no pain, no gain!!

UK
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Something more like:

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

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Madrid
Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Simple and works perfect!!

Thanks a lot!
Logged

no pain, no gain!!

East Anglia (UK)
Online Online
Faraday Member
**
Karma: 114
Posts: 4267
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I was going to suggest using the atoi function
Code:
String red = inComming[6]+inComming[7]+inComming[8];
int redValue = atoi(red);
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

UK
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I was going to suggest using the atoi function
Code:
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.
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Pages: [1]   Go Up
Jump to: