Go Down

Topic: Trying to wrap my head around converting int to ascii (itoa?). (Read 846 times)previous topic - next topic

tinker_camp

Please excuse me for being a noob:

Trying to write a clock that uses only one seven segment LED (you know, for fun), so I am trying to do a conversion from an integer supplied by minute() (from the Time library) into an array of single digits.  For instance, to convert, say 42 into an array that is like myminute[3]={'4','2'} or even better into int myminute[3]={4,2}.

I thought I had read the itoa manual correctly thinking that  itoa(minute(),myminute,3) would yield the desired affect, but not!

So, a little prodding for this old, balding perl programmer would be helpful.

Blake

gardner

Well, you can do it the "hard way" -- something like:

Code: [Select]
`char myminute[3];int minute = 42;myminute[1] = minute % 10 + '0';minute /= 10;myminute[0] = minute % 10 + '0';myminute[2] = '\0';`

Or maybe use sprintf -- which works just like in perl

Code: [Select]
`sprintf(myminute, "%.02d", minute);`

tinker_camp

sprint-freaking-f!

Thanks Gardner!

bperrybap

#3
Jan 11, 2013, 05:45 amLast Edit: Jan 11, 2013, 05:47 am by bperrybap Reason: 1
For a clock using seven segment digits you really don't need or want ascii.
What you really want is the individual digits.
Then you can use the digits to index into a table that has the information
about which segments to turn on for that digit.
Or if you are using one of the 7 segment libraries out there, they also typically
want the digit value (not ascii) that is to be displayed on the 7 segment display which in turn they
use to look up which segments to turn on for that digit.

To get the digits from a 2 digit value like hours/min/seconds
you can use some simple divide by 10 and module by 10 similar
to what gardner showed only skipt converting the digit to ascii.

i.e.

tensdigit = value / 10;
onesdigit = value % 10;

The challenging part when only using a single LED digit for display
(vs 4 or 6 digits)
so you can tell the fields apart.
i.e. when you see the digit changing, how do you know which digits are hours vs mins etc...

--- bill

holmes4

Code: [Select]
`itoa(minute(),myminute,3)`

should have been

Code: [Select]
`itoa(minute(),myminute,10)`

The last param is the base so 10 for decimal.

Mark

Krupski

tensdigit = value / 10;
onesdigit = value % 10;

That only works for 2 digits. A generic version would be:

Code: [Select]
`int value = 1234;char ones_place = ((value / 1) % 10);char tens_place = ((value / 10) % 10);char hund_place = ((value / 100) % 10);char thou_place = ((value / 1000) % 10);// etc.....`
Gentlemen may prefer Blondes, but Real Men prefer Redheads!

bperrybap

tensdigit = value / 10;
onesdigit = value % 10;

That only works for 2 digits. A generic version would be:

Code: [Select]
`int value = 1234;char ones_place = ((value / 1) % 10);char tens_place = ((value / 10) % 10);char hund_place = ((value / 100) % 10);char thou_place = ((value / 1000) % 10);// etc.....`

Ok.... but I've not ever seen a clock that needs to display a number larger than 59  for minutes

Back to the initial ascii question.
Why would you want to have ascii digits when pushing time/clock digits to a seven segment display?

Krupski

Ok.... but I've not ever seen a clock that needs to display a number larger than 59  for minutes

Back to the initial ascii question.
Why would you want to have ascii digits when pushing time/clock digits to a seven segment display?

I posted that because there are other uses for extracting digits from a value. Of course a clock only needs 2!

For the clock app, if I were doing it I would make an 10 byte x 7 bit array of segment patterns 0 thru 9 and use the extracted values as an index into the array, then use that data to light the proper segments.
Gentlemen may prefer Blondes, but Real Men prefer Redheads!

dhenry

Quote
Or maybe use sprintf

Yes, but very costly in terms of space / execution.

Go Up

Please enter a valid email to subscribe