Go Down

### Topic: convert int to string and then... (Read 6362 times)previous topic - next topic

#### donde

##### Oct 10, 2012, 04:17 pm
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.

#### PaulS

#1
##### Oct 10, 2012, 04:26 pm
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: [Select]
`int n = 354;int h = n / 100; // h = 3int t = (n / 10) % 10; // 354 / 10 = 35. 35%10 = 5int o = n % 100; // 354 % 100 = 4`
The art of getting good answers lies in asking good questions.

#### donde

#2
##### Oct 10, 2012, 04:46 pm
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.

#### donde

#3
##### Oct 10, 2012, 11:11 pm
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()

#### Arrch

#4
##### Oct 10, 2012, 11:18 pmLast Edit: Oct 10, 2012, 11:21 pm by Arrch Reason: 1

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: [Select]
`359 % 100 % 5 = 4`

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

Code: [Select]
`int o = n % 10`

#### zdillman

#5
##### Oct 11, 2012, 01:34 am
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!

#### Arrch

#6
##### Oct 11, 2012, 01:41 am

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

#### Jimmy60

#7
##### Oct 11, 2012, 02:59 am
Code: [Select]
` 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[].

#### donde

#8
##### Oct 12, 2012, 06:55 am
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

Go Up