Solved

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

Posted on 2011-03-02
3
378 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

759 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now