Solved

an INT --> string

Posted on 1998-07-16
9
284 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 1

Author Comment

by:mrquija
Comment Utility
Adjusted points to 15
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 2

Expert Comment

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

Expert Comment

by:sganta
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks for the help!
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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…
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 how to create, access, and change arrays in the C programming language.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now