Pages: [1]   Go Down
Author Topic: Convert Array of chars to integer except the first position  (Read 611 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 0
Posts: 106
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello, i'm trying to convert a message received with VirtualWire library that starts with an 'R' to an integer, but at this time i couldn't find an explanation to this.
The message received is like "R016" and it is stored in a array like message[3] and i want to convert message to integer (like atoi function does) but except the letter 'R'.

Can you please give me some help?? THANKS in advance!!
Logged

Offline Offline
God Member
*****
Karma: 17
Posts: 754
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Use atoi but pass it a pointer to the second element in the array instead of the first.
Logged

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3379
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello, i'm trying to convert a message received with VirtualWire library that starts with an 'R' to an integer, but at this time i couldn't find an explanation to this.
The message received is like "R016" and it is stored in a array like message[3] and i want to convert message to integer (like atoi function does) but except the letter 'R'.

Can you please give me some help?? THANKS in advance!!

How can four letters and a null terminating character be stored in a 3 byte array?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26248
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Exactly what they both said.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

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

I'll second that motion smiley-wink

It should be message[5], for 4 characters plus terminating null.

And you would use

Code:
int value = atoi(message+1);

Or, more explicitly:
Code:
int value = atoi(&message[1]);
Logged

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

Offline Offline
Full Member
***
Karma: 0
Posts: 106
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the reply! I've corrected the size of array to 4 (message[4]) to store the Rxxx value (forget the \r).
So i basically need to use atoi function and point the beggining of convertion to position [1], is that what int value = atoi (message + 1) do, is that right??
Logged

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

Thanks for the reply! I've corrected the size of array to 4 (message[4]) to store the Rxxx value (forget the \r).
So i basically need to use atoi function and point the beggining of convertion to position [1], is that what int value = atoi (message + 1) do, is that right??
There is no \r

\r is "carriage return".  \0 is the end of string marker.

So you have Rxxx\0 - count them... R  x  x  x  \0  - I make that 5.  Not 4, nor 3.  5.

The variable "message" points to the start of the memory set aside as 5 bytes, so message+1 points to the start plus 1.  It is functionally the same as using the & operator (address of) on the second element (message[1]) of the array (&message[1] is "the address of the second element (number 1 counting from 0 - 0, 1, 2, 3, 4) in the array").
Logged

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

Offline Offline
Full Member
***
Karma: 0
Posts: 106
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

message[0] = R
message[1] = x
message[2] = x
message[3] = x
message[4] = \O

5 bytes: message[5] is the correct form, confirm

int value = atoi(&message[1])  or  int value = atoi(message + 1)   -----> is the same?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26248
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
[code]int value = atoi(&message[1])
 or  int value = atoi(message + 1)   -----> is the same?[/code]

Yes, they're equivalent.

Also, surprisingly is
Code:
int value = atoi(&1[message])
« Last Edit: February 22, 2013, 10:52:04 am by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Full Member
***
Karma: 0
Posts: 106
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you all very much! You're really helpfull.
Logged

Pages: [1]   Go Up
Jump to: