Solved

an INT --> string

Posted on 1998-07-16
9
292 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
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
ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

 
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 10 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

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…
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 opening and reading files 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.

831 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