Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 435
  • Last Modified:

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
Asked:
logicallayer
  • 2
1 Solution
 
Kent OlsenData 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
 
logicallayerAuthor 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
 
Kent OlsenData 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

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now