Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

an INT --> string

Posted on 1998-07-16
9
Medium Priority
?
301 Views
Last Modified: 2010-04-15
Is there a way that I can COPY a integer type var into a character array?

example,

int dummy = 10;
char string1[maxlen];
int i;
...
<copy dummy into string1>
...

so that when I do a

for (i=0; i<maxlen;i++) printf ("%c", string1[i]);

I will get :
10

basically, I'm just trying to get the contents of
dummy == string1[];

suggestions?
0
Comment
Question by:mrquija
[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
9 Comments
 
LVL 2

Expert Comment

by:duneram
ID: 1251727
Hi,

There happens to be a fairly easy method for doing this its with the sprintf function.
If you are programming in windows its better to use wsprintf.

To use sprintf you would do this:

int dummy = 10
char String[maxlen];

sprintf(maxlen,"%d", dummy);  /* places the string "10" into maxlen */


Hope this helps!!


0
 
LVL 2

Expert Comment

by:duneram
ID: 1251728
I mistyped...

the sprintf should have been


sprintf(String,"%d", dummy);  /* places the string "10" into String */

I was treating the 'maxlen' like a char maxlen[100] or something along those lines.


0
 
LVL 1

Author Comment

by:mrquija
ID: 1251729
argh..got confused with your statement

What type is String?? Let's say I wrote it like this


int dummy=10;
char stringvalue[100];

sprintf(stringvalue, "%d", dummy);  //  This places "dummy"'s value (10) into the character
                                                       array "stringvalue"?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 1

Author Comment

by:mrquija
ID: 1251730
Adjusted points to 15
0
 
LVL 2

Expert Comment

by:Slarti
ID: 1251731
In answer to your commented answer, yes. duneram deserves the points, why did you flunk him?
0
 
LVL 4

Expert Comment

by:sganta
ID: 1251732
Hai,

You can do in 2 ways.

Method 1
-------------
   string1 = itoa(dummy);


Method 2:
-------------
  int i,j=0,k;
  i = dummy;
  char string[100],string1[100]
  while ( i > 0 )
  {
     string[j++] = i%10+48;
     i /= 10;
  }
  string[j++] = '\0';
  i = 0;
  j--
  while (j >= 0)
  {
     string1[i++] = string[j];
     j--;
  }
  string1[j] = '\0';
  /* here string1 contains the actual value in string */
  printf( "Character value in array is %s.",string1);
}

Good luck.
0
 
LVL 1

Author Comment

by:mrquija
ID: 1251733
Slarti:  I didn't flunk him on purpose..I totally forgot that you can acccept the answer and post a comment afterwards. Take a chill pill. Duneram totally deserves the points..why do you think I adjusted the points from 10-15..cuz he actually helped me out instead of the standard answers of "I dont know" or the answers that give you the most complicated solutions to a simple problem. And I was waiting for him to respond to give him the points.  Thank you for your ignorance.

Sganta:  Duneram gave the information that I needed already. I was merely asking him a question to make sure that I understood what he had written. Sorry.

0
 
LVL 2

Accepted Solution

by:
duneram earned 40 total points
ID: 1251734
HI,

I just got back from the Red Cross where I am involved in their apheresis donation program (each donation takes a couple of hours but it helps cancer patients, so its a good thing).

Thats why I am just getting back now.  Yeah, I was just using the original variable names you used (or tried to, even though I mistyped it).  

I guess 'String' was a misleading variable name it could have been
char x[100];  or whatever you pick.

Another interesting function to look at (totally off the subject) but you may need it someday is

strftime().   <--- this function lets you take a time value (like time of day or a date) and format it into a string.   When I worked on my calendar function back in 93 it would have saved me oodles of time if I had known about that function.

Good Luck

David
0
 
LVL 1

Author Comment

by:mrquija
ID: 1251735
Thanks for the help!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
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 and use conditional statements in the C programming language.
Suggested Courses

636 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