Solved

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

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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 and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Suggested Courses

752 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