Pages: [1]   Go Down
Author Topic: convert int to string and then...  (Read 2836 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 46
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

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

Quote
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.
Code:
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
Logged

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

Quote
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.
Logged

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

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()
Logged

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

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:

Code:
359 % 100 % 5 = 4

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

Code:
int o = n % 10
« Last Edit: October 10, 2012, 04:21:10 pm by Arrch » Logged

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

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!
Logged

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

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
Logged

Saskatchewan
Offline Offline
Sr. Member
****
Karma: 19
Posts: 364
When the going gets weird, the weird turn pro. - Hunter S. Thompson
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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[].
Logged

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

Quote
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   smiley
Logged

Pages: [1]   Go Up
Jump to: