Pages: [1]   Go Down
Author Topic: Trying to wrap my head around converting int to ascii (itoa?).  (Read 567 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Ontario
Offline Offline
God Member
*****
Karma: 24
Posts: 860
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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:
sprintf(myminute, "%.02d", minute);
Logged

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

sprint-freaking-f!

Thanks Gardner!
Logged

Dallas, TX USA
Offline Offline
Faraday Member
**
Karma: 63
Posts: 2636
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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)
will be to figure out the cadence/spacing/delay/blinking between displaying 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
« Last Edit: January 10, 2013, 11:47:12 pm by bperrybap » Logged

Poole, Dorset, UK
Offline Offline
Edison Member
*
Karma: 50
Posts: 2197
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
itoa(minute(),myminute,3)

should have been

Code:
itoa(minute(),myminute,10)

The last param is the base so 10 for decimal.

Mark
Logged

Worst state in America
Offline Offline
God Member
*****
Karma: 32
Posts: 792
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


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

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

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

Gentlemen may prefer Blondes, but Real Men prefer Redheads!

Dallas, TX USA
Offline Offline
Faraday Member
**
Karma: 63
Posts: 2636
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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

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

Code:
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  smiley-wink

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

Worst state in America
Offline Offline
God Member
*****
Karma: 32
Posts: 792
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


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

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

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

Gentlemen may prefer Blondes, but Real Men prefer Redheads!

Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Or maybe use sprintf

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

Pages: [1]   Go Up
Jump to: