Pages: [1]   Go Down
Author Topic: how to retrieve some value from a value?  (Read 726 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hello i have a question about how to retrieve some value from a value?

so this my problem...

Code:
int val = 123;

if (val == 2) <=== val '2' is part of val '123'
{
\\this will run
}

or

Code:
char str = abc;

if (str == c)  <=== str 'c' is part of str 'abc'
{
\\this will run
}


what is function to do that?
« Last Edit: May 18, 2012, 09:05:51 pm by ase123 » Logged

Offline Offline
Edison Member
*
Karma: 19
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

There is none.

For numbers, you can use a combination of integer division and modulo to find out.
For strings, you could use the strchr function.
Logged

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 33
Posts: 1287
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Strictly speaking you are not correct.

If a numeric variable has a value it only has that value. So if val=123 then 2 is not 'part' of the number, is is one of the values that val could be. In your example, val==2 would fail. Maybe it is just the words you used, but you need to be clear on this.

The other example you have is different because you have an string, which in C/C++ is an array of characters. So what you are really looking at is str = "abc" (note you need the quotation marks), which looks like the characters 'a', 'b', 'c' and '\0' in sequence in an array. The last character is called the NUL character and marks the end of every string. In this case it makes sense to see 'b' as a part of str as you can extract or change it.

There are a whole set of functions (string handling) that allow parsing of strings and subcomponents. Look for "string functions", there are too many to list here, to get familiar with what they do. You will need to inlude the header file <string.h> in your code if you use them.
Logged

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 653
Posts: 50881
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you have a 3 digit value, like 123, you can get the hundredths, tenths and ones values easily enough.
Code:
int val = 123;

int ones = val % 100; // ones = 3
int rest = val/10; // rest = 12
int tens = rest % 10; tens = 2
int huns = rest / 10; huns = 1

Some adjustments need to be made if the value is a 4 digit or 5 digit value, obviously.
Logged

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

For strings, you could use the strchr function.

can you give example of strchr please?
if i have a char = "abc" how i get "c" in this char?

@PaulS
that's not what I mean

example...
Code:
int val = analogRead(11);

if (val == 1)  <=== if there is byte '1' in val then LED1 ON >> example val = 213 from analogRead
{
digitalWrite(LED1, HIGH)
}
else         <=== if there is not byte '1' in val then LED1 OFF >> example val = 253 from analogRead
{
digitalWrite(LED1, LOW)
}

if (val == 5)  <=== if there is byte '5' in val then LED5 ON >> example val = 513 from analogRead
{
digitalWrite(LED5, HIGH)
}
else            <=== if there is not byte '5' in val then LED5 OFF >> example val = 673 from analogRead
{
digitalWrite(LED5, LOW)
}

thats what i mean.. so i want to get part of value from a value
« Last Edit: May 18, 2012, 10:42:54 pm by ase123 » Logged

California
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3447
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
can you give example of strchr please?
if i have a char = "abc" how i get "c" in this char?
Google has plenty of examples.

Quote
@PaulS
that's not what I mean

example...
Code:
int val = analogRead(11);

if (val == 1)  <=== if there is byte '1' in val then LED1 ON >> example val = 213 from analogRead
{
digitalWrite(LED1, HIGH)
}
else         <=== if there is not byte '1' in val then LED1 OFF >> example val = 253 from analogRead
{
digitalWrite(LED1, LOW)
}

if (val == 5)  <=== if there is byte '1' in val then LED5 ON >> example val = 513 from analogRead
{
digitalWrite(LED5, HIGH)
}
else            <=== if there is not byte '5' in val then LED5 OFF >> example val = 673 from analogRead
{
digitalWrite(LED5, LOW)
}

thats what i mean.. so i want to get part of value from a value

So you either need to do with PaulS said and check each digit, or use itoa() to convert it to a string and do it the same way as a string.
Logged

Global Moderator
Netherlands
Online Online
Shannon Member
*****
Karma: 227
Posts: 14026
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
can you give example of strchr please?
if i have a char = "abc" how i get "c" in this char?
- http://www.cplusplus.com/reference/clibrary/cstring/strchr/ -

this sites explains very much about the C++ language used in the Arduino.
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

New Jersey
Offline Offline
Faraday Member
**
Karma: 72
Posts: 3761
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you're looking for a particular digit in any position in a numeric value, you might consider using sprintf to convert the number to a string and then use strchr as suggested for "abc".
Logged

Pages: [1]   Go Up
Jump to: