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
Solved

an INT --> string

Posted on 1998-07-16
9
295 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

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

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…
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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

856 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