Solved

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

Posted on 2011-03-02
3
429 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
[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
3 Comments
 
LVL 45

Accepted Solution

by:
Kent Olsen 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:Kent Olsen
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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.
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.

617 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