Go Down

### Topic: how to retrieve some value from a value? (Read 1 time)previous topic - next topic

#### ase123

##### May 19, 2012, 04:04 amLast Edit: May 19, 2012, 04:05 am by ase123 Reason: 1
hello i have a question about how to retrieve some value from a value?

so this my problem...

Code: [Select]
`int val = 123;if (val == 2) <=== val '2' is part of val '123'{\\this will run}`

or

Code: [Select]
`char str = abc;if (str == c)  <=== str 'c' is part of str 'abc'{\\this will run}`

what is function to do that?

#### WizenedEE

#1
##### May 19, 2012, 04:21 am
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.

#### marco_c

#2
##### May 19, 2012, 04:29 am
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.
Arduino Libraries http://arduinocode.codeplex.com
Parola for Arduino http://parola.codeplex.com
Arduino++ blog https://arduinoplusplus.wordpress.com

#### PaulS

#3
##### May 19, 2012, 04:35 am
If you have a 3 digit value, like 123, you can get the hundredths, tenths and ones values easily enough.
Code: [Select]
`int val = 123;int ones = val % 100; // ones = 3int rest = val/10; // rest = 12int tens = rest % 10; tens = 2int huns = rest / 10; huns = 1`

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

#### ase123

#4
##### May 19, 2012, 05:10 amLast Edit: May 19, 2012, 05:42 am by ase123 Reason: 1

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: [Select]
`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

#### Arrch

#5
##### May 19, 2012, 05:17 am
Quote
can you give example of strchr please?
if i have a char = "abc" how i get "c" in this char?

Quote
@PaulS
that's not what I mean

example...
Code: [Select]
`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.

#### robtillaart

#6
##### May 19, 2012, 12:06 pm
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.
Rob Tillaart

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

#### wildbill

#7
##### May 19, 2012, 12:19 pm
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".

Go Up

Please enter a valid email to subscribe