# convert int to string and then...

I want to convert 3 digit integer like, 354 to string so to dig out each character (number) to convert back to integer. I'll use each number to show on single 7 segment LCD, sequentially, delay about 250ms. I already know how to show on display.

Not sure how to make 3 digits a string array, to get at each character, like char[0], char[1], char[2], and then convert each array element back to an integer to use in display.

I want to convert 3 digit integer like, 354 to string so to dig out each character (number) to convert back to integer.

You could use itoa() to convert the value to a string, and then subtract ‘0’ from each character in the array to get the number, in each place, but division and modulo will be faster.

``````int n = 354;
int h = n / 100; // h = 3
int t = (n / 10) % 10; // 354 / 10 = 35. 35%10 = 5
int o = n % 100; // 354 % 100 = 4
``````

but division and modulo will be faster.
Code:

I was kind of thinking of modulo, but WOW thanks for jelling my thought! Your example looks good. Thanks.

Well, the last statement didn't work.
It should be: int o = (n % 100) % 5; // 354 % 100 % 5 = 4

I should have stated, not for just 354, but for any number. So, I guess I should look into itoa()

donde:
Well, the last statement didn't work.
It should be: int o = (n % 100) % 5; // 354 % 100 % 5 = 4

I should have stated, not for just 354, but for any number. So, I guess I should look into itoa()

That won't work either:

``````359 % 100 % 5 = 4
``````

There was a small typo in PaulS' post. What you need is

``````int o = n % 10
``````

I just found your post after posting my question. Which is essentially the same thing. The above does work for 354 but I also need it for any number. If anyone could think of a way to do this please share! I was thinking to convert the integer into a string, and then from the string to either an integer or character array. Not quite sure how to do this, but it may help!

zdillman:
I just found your post after posting my question. Which is essentially the same thing. The above does work for 354 but I also need it for any number. If anyone could think of a way to do this please share! I was thinking to convert the integer into a string, and then from the string to either an integer or character array. Not quite sure how to do this, but it may help!

The above does work for any 3 digit number

``````	unsigned long number = 123662012;     // assign the value you need to number
byte n[10];                           // to keep results 10 digits max
byte i = 0;                           // to index array
while (number)
{
n[i] = number % 10;
number /= 10;
i++;
}
``````

A different approach that came to me. This will store the digits from a number as elements of array n from least to most significant. It simply uses modulo to get the current ones digit then uses divide to drop that digit. When the loop runs out of number n contains the digits and i has the count and number is gone so save it you don't want to lose it.

Obviously only works on integers and the size is only limited by the size of array n.

There was a small typo in PaulS' post. What you need is

Code:

int o = n % 10

Well thanks to all for ideas. I wound up using PaulS modulo suggestion plus the correction of Arrch, which really helped. Now any 3 digit number can be operated on as digit1, digit2, digit 3. SUPER