x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 438

# convert int to char (ex int 59 to ch1 ='5' , ch2='9'?

I was wondering if I can do it to convert a two digit int to a char for each digit

for example I have a clock that can go up to 59, I want assign the 5 to ch1 and the 9 to ch2

I was thinking to use bits but considering we are talking about awhole 16bits which int has, it would be hard to get the actual number....

there must be a way in which I treat an int as if it's an array of chars or something...

0
logicallayer
• 2
1 Solution

Data Warehouse Architect / DBACommented:
Hi LL,

Pretty easy, once you know how.  :)

char Digits[3];
int  SomeValue;

Digits[2] = 0;
Digits[1] = SomeValue % 10;
SomeValue = SomeValue / 10;
Digits[0] = SomeValue % 10;

or

sprintf (Digits, "%2.2d", SomeValue);

Good Luck,
Kent
0

Author Commented:
interesting,
although I just found
itoa(int,string,base) will do the job for me
http://www.cplusplus.com/reference/clibrary/cstdlib/itoa/

but I was actually trying to figure out some way similar to yours,

/clap
impressed it's nice.

0

Data Warehouse Architect / DBACommented:
Hi LL,

Depending on how finely we slice we slice the subtleties, there are probably a dozen ways to do this in C.

Kent
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.