Solved

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

Posted on 2011-03-02
3
399 Views
Last Modified: 2012-05-11
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
Comment
Question by:logicallayer
  • 2
3 Comments
 
LVL 45

Accepted Solution

by:
Kdo earned 500 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

by:logicallayer
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 45

Expert Comment

by:Kdo
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

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…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

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

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

Join & Ask a Question