Solved

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

Posted on 2011-03-02
Medium Priority
431 Views
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
Question by:logicallayer
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• Learn & ask questions
• 2

LVL 46

Accepted Solution

Kent Olsen earned 2000 total points
ID: 35024001
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 Comment

ID: 35024048
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

LVL 46

Expert Comment

ID: 35026610
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

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourtâ€¦
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and infâ€¦
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
###### Suggested Courses
Course of the Month10 days, 21 hours left to enroll

#### 719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.