Read Part Of An Int

hey guys

i need some help, i need to read part of an INT, for example if i had a

int status = 12345;

i want to be able to put

statusInfo(3)

and get back the third character of that list of ints

any help is appreciated

int status = 12345;
 hundredths = (status / 100) % 10 ;

The % gives you the remainder after a division in this case by 10.

What's wrong with a byte array?

One important thing to note:

"12345" is merely the decimal representation of an integer value -- you'd use that representation to write that value in C++ code, and you can e.g. get the Serial.print() method to render the integer value as that sequence of characters, but an integer value by itself actually doesn't have an inherent representation.

So what you're actually asking for is "the third character of the decimal representation of that integer value".

Given that statement, the straightforward approach probably seems to be to first render the decimal representation of your integer value into a char array, then take the third character from that, but it's a lot more efficient (and easier) to simply calculate it the way Grumpy_Mike has shown.

Jiggy-Ninja: What's wrong with a byte array?

Functionally, there's nothing wrong with it, but it's less efficient and more cumbersome to code if you need just a single digit.

Using something like the itoa() function to render the integer value as a decimal into a char array basically performs exactly the calculation shown by Grumpy_Mike, but for all digits, just so you can use one of them.

Even if you'd need all digits in turn, it'll be more efficient to do repeatedly do "digit = status % 10;" (to get the rightmost decimal digit) and "status /= 10;" (to shift status to the right by one decimal place) to get the digits one after another.

[quote author=Michael Buschbeck link=topic=192820.msg1424918#msg1424918 date=1381506692] "the third character of the decimal representation of that integer value". [/quote]

yes this is exactly what i mean, sorry about the confusion

so i have a 4 digit number (1010) which i need to know the 1st, 2nd, 3rd and 4th character of individually, im sorry but i dont understand what you are trying to do with your code Grumpy_Mike, can you explain?

im sorry but i dont understand what you are trying to do with your code

I am not trying to do anything I am doing it. Suppose you had a calculator with the number 12345 on it. If you want to know how many hundreds are in that number you simply count the third number from the left. I a program if you divide that number by 100 the result on your calculator will be 123.45. But the computer uses integers so there is nothing after the decimal point so dividing by 100 you get 123. If you divide this by 10 you will get 12 remainder 3. The % operation returns the remainder of a division so 123 % 10 gives you 3, which is the number you were after. To get any other position just change the first division and keep the % 10 the same.

right, now i get it! thanks for the help guys! :D