|
From: Khalid El-K. <kha...@ho...> - 2003-04-01 02:42:57
|
Hi fellows, I'm wondering if there's a possible way to turn an integer to its string representation in C //I want to get a string "5" out of f int f=5; thanx in advance. Regards, Khalid Al-Kary _________________________________________________________________ The new MSN 8: advanced junk mail protection and 2 months FREE* http://join.msn.com/?page=features/junkmail |
|
From: Ioannis V. <iv...@em...> - 2003-04-01 03:13:29
|
> -----Original Message----- > From: dev...@li... > [mailto:dev...@li...] On Behalf > Of Khalid El-Kary > Sent: Tuesday, April 01, 2003 5:43 AM > To: Dev...@li... > Subject: [Dev-C++] integer to string > > > Hi fellows, > > I'm wondering if there's a possible way to turn an integer to > its string > representation in C > > //I want to get a string "5" out of f > int f=5; > > thanx in advance. > > Regards, > Khalid Al-Kary #include <stdio.h> /* ... */ int f=7; char s[5]; sprintf(s, "%d", f); Ioannis Vranos * Programming pages: http://www.noicys.freeurl.com * Alternative URL: http://run.to/noicys |
|
From: Luigi S. <gi...@li...> - 2003-04-01 08:23:18
|
On Tue, Apr 01, 2003 at 02:42:48AM +0000, Khalid El-Kary wrote:
> Hi fellows,
>
> I'm wondering if there's a possible way to turn an integer to its string
> representation in C
>
> //I want to get a string "5" out of f
> int f=5;
>
> thanx in advance.
>
> Regards,
> Khalid Al-Kary
>
Hello Khalid,
You have already been supplied with an excellent solution, here is
another approach, both: less efficient and less elegant; but I'm posting
it anyway because I feel it is an example some newbies (like me) can
probably benefit from.
t_itoa.c
--------
#include <stdio.h>
#include <stdlib.h>
#include "itoa.h"
/********************************************************************/
int
main()
{
int k = 123; /* integer value to be converted */
char *s = itoa(k); /* string holding the converted value */
printf("INPUT:\nk = %d\n\n", k);
printf("OUTPUT:\ns = %s\n\n", s);
free(s); /* return memory to the system, as soon as you can */
k = -4567; /* a little harder: using a negative number */
s = itoa(k);
printf("INPUT:\nk = %d\n\n", k);
printf("OUTPUT:\ns = %s\n\n", s);
free(s);
return 0;
}
--------
itoa.h
------
#ifndef itoa_h
#define itoa_h
#pragma defs_in itoa.c /* needed by makefile generator (ignore it) */
/********************************************************************/
/* turn an integer into a string (i.e. an array) */
char *
itoa(int n);
/* invert order of elements in an array */
char *
invert(char *s);
/********************************************************************/
#endif /* not itoa_h */
------
itoa.c
------
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include "itoa.h"
#define MAXSZ 21 /* assuming integer is made of 19 digits + sign */
/********************************************************************/
char *
itoa(int n)
{
char * buf = (char *)calloc(MAXSZ, sizeof(char)); /* get memory */
char *p = buf; /* pointer used to sweep across buffer */
char neg = 0; /* 1 means that the integer is negative */
assert(buf != 0); /* check if memory was allocated */
if (n < 0) /* negative integer */
{
n = -n; /* work on its absolute value */
neg = 1; /* turn on the flag */
}
while (n) /* main loop */
{
*p++ = '0' + n % 10; /* char for a digit as offset from '0' */
n /= 10; /* drop last character */
}
if (neg) /* append a negative sign */
*p++ = '-';
*p = '\0'; /* terminate string */
return invert(buf);
}
/********************************************************************/
char *
invert(char *s)
{
char *p = s + strlen(s); /* points the terminating '\0' */
char *q = s - 1; /* points one before first char */
while (--p > ++q)
{
char c = *p; /* classic code used to swap characters */
*p = *q;
*q = c;
}
return s;
}
------
Regards
--
>> gisan >>
|
|
From: Per W. <pw...@ia...> - 2003-04-01 08:38:44
|
Your implementation of itoa would leak memory - normally itoa would use a
static buffer that is overwritten at each call.
A better itoa would build the buffer from the end, i.e. *p-- = ch
thereby removing need for reversing the string.
/Per W
On Tue, 1 Apr 2003, Luigi Santillo wrote:
> On Tue, Apr 01, 2003 at 02:42:48AM +0000, Khalid El-Kary wrote:
> > Hi fellows,
> >
> > I'm wondering if there's a possible way to turn an integer to its string
> > representation in C
> >
> > //I want to get a string "5" out of f
> > int f=5;
> >
> > thanx in advance.
> >
> > Regards,
> > Khalid Al-Kary
> >
>
> Hello Khalid,
>
> You have already been supplied with an excellent solution, here is
> another approach, both: less efficient and less elegant; but I'm posting
> it anyway because I feel it is an example some newbies (like me) can
> probably benefit from.
>
> t_itoa.c
> --------
> #include <stdio.h>
> #include <stdlib.h>
> #include "itoa.h"
>
> /********************************************************************/
> int
> main()
> {
> int k = 123; /* integer value to be converted */
> char *s = itoa(k); /* string holding the converted value */
>
> printf("INPUT:\nk = %d\n\n", k);
> printf("OUTPUT:\ns = %s\n\n", s);
> free(s); /* return memory to the system, as soon as you can */
>
> k = -4567; /* a little harder: using a negative number */
> s = itoa(k);
> printf("INPUT:\nk = %d\n\n", k);
> printf("OUTPUT:\ns = %s\n\n", s);
> free(s);
>
> return 0;
> }
> --------
>
>
>
>
>
> itoa.h
> ------
> #ifndef itoa_h
> #define itoa_h
> #pragma defs_in itoa.c /* needed by makefile generator (ignore it) */
> /********************************************************************/
>
> /* turn an integer into a string (i.e. an array) */
> char *
> itoa(int n);
>
> /* invert order of elements in an array */
> char *
> invert(char *s);
>
> /********************************************************************/
> #endif /* not itoa_h */
> ------
>
>
>
>
> itoa.c
> ------
> #include <stdlib.h>
> #include <assert.h>
> #include <string.h>
> #include "itoa.h"
>
> #define MAXSZ 21 /* assuming integer is made of 19 digits + sign */
> /********************************************************************/
> char *
> itoa(int n)
> {
> char * buf = (char *)calloc(MAXSZ, sizeof(char)); /* get memory */
> char *p = buf; /* pointer used to sweep across buffer */
> char neg = 0; /* 1 means that the integer is negative */
>
> assert(buf != 0); /* check if memory was allocated */
>
> if (n < 0) /* negative integer */
> {
> n = -n; /* work on its absolute value */
> neg = 1; /* turn on the flag */
> }
>
> while (n) /* main loop */
> {
> *p++ = '0' + n % 10; /* char for a digit as offset from '0' */
> n /= 10; /* drop last character */
> }
>
> if (neg) /* append a negative sign */
> *p++ = '-';
>
> *p = '\0'; /* terminate string */
> return invert(buf);
> }
>
> /********************************************************************/
> char *
> invert(char *s)
> {
> char *p = s + strlen(s); /* points the terminating '\0' */
> char *q = s - 1; /* points one before first char */
>
> while (--p > ++q)
> {
> char c = *p; /* classic code used to swap characters */
> *p = *q;
> *q = c;
> }
>
> return s;
> }
> ------
>
>
> Regards
> --
> >> gisan >>
>
>
> -------------------------------------------------------
> This SF.net email is sponsored by: ValueWeb:
> Dedicated Hosting for just $79/mo with 500 GB of bandwidth!
> No other company gives more support or power for your dedicated server
> http://click.atdmt.com/AFF/go/sdnxxaff00300020aff/direct/01/
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|
|
From: Luigi S. <gi...@li...> - 2003-04-01 14:10:09
|
On Tue, Apr 01, 2003 at 11:37:28AM +0200, Per Westermark wrote: > Your implementation of itoa would leak memory - normally itoa would use a > static buffer that is overwritten at each call. I agree that mine is just a try, also I wish to learn from your comment. My code has a memory leak because of: - user will almost always forget to call "free" (shortly: bad designed code) - there is a bug in the code (shortly: bugged code) - both bad design and bad code By the way, a call to "strcpy" does copy the piece of an array up-to the '\0', does the function "free" return memory up-to the '\0' or does it free the whole array (as I assume) ? > > A better itoa would build the buffer from the end, i.e. *p-- = ch > thereby removing need for reversing the string. I'll try to follow this advice too. > /Per W Thanks Per -- >> gisan >> |
|
From: Per W. <pw...@ia...> - 2003-04-01 14:30:02
|
free() ignores the contents of your array. All you need is the pointer you originally received from malloc() or calloc(). free() will then release the full block - unless you for some reason has managed to break the heap by writing outside of the allocated area (or actually to write immediately BEFORE the returned pointer) and thereby destroy the heap management that is almost always part of the allocated memory block. /Per W On Tue, 1 Apr 2003, Luigi Santillo wrote: > On Tue, Apr 01, 2003 at 11:37:28AM +0200, Per Westermark wrote: > > Your implementation of itoa would leak memory - normally itoa would use a > > static buffer that is overwritten at each call. > > I agree that mine is just a try, also I wish to learn from your > comment. My code has a memory leak because of: > - user will almost always forget to call "free" (shortly: bad designed code) > - there is a bug in the code (shortly: bugged code) > - both bad design and bad code > > By the way, a call to "strcpy" does copy the piece of an array up-to > the '\0', does the function "free" return memory up-to the '\0' or does > it free the whole array (as I assume) ? > > > > A better itoa would build the buffer from the end, i.e. *p-- = ch > > thereby removing need for reversing the string. > > I'll try to follow this advice too. > > > /Per W > > Thanks Per > -- > >> gisan >> > > > ------------------------------------------------------- > This SF.net email is sponsored by: ValueWeb: > Dedicated Hosting for just $79/mo with 500 GB of bandwidth! > No other company gives more support or power for your dedicated server > http://click.atdmt.com/AFF/go/sdnxxaff00300020aff/direct/01/ > _______________________________________________ > Dev-cpp-users mailing list > Dev...@li... > TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > |
|
From: Ricardo R. <ri...@po...> - 2003-04-01 08:52:07
|
perhaps this example can help you a little more.
Ricardo.
**********ITOA/ATOI **********
#include <stdio.h>
#include <stdlib.h>
int main()
{
char cad[20];
/* the transformation: integer --> string */
itoa(2003,cad,10); // 2003 is the number to store in cad[]
printf(cad); // in decimal base = 10.
/* and the inverse transformation: string --> integer */
printf("\n %d",atoi(cad)); // cad[ ] is transformed to integer
printf("\n\n");
system("PAUSE");
return 0;
}
*************************************
|
|
From: Per W. <pw...@ia...> - 2003-04-01 09:14:20
|
Note that while itoa is nonstandard, some implementations take two
parameters, and some take 3.
The third parameter is then number base, i.e. 2 for binary, 8 for octal,
etc.
/Per W
On Tue, 1 Apr 2003, Ricardo L=F3pez Ruiz wrote:
> perhaps this example can help you a little more.
> Ricardo.
>
>
> **********ITOA/ATOI **********
>
> #include <stdio.h>
> #include <stdlib.h>
>
> int main()
> {
> char cad[20];
>
> /* the transformation: integer --> string */
> itoa(2003,cad,10); // 2003 is the number to store in cad[]
> printf(cad); // in decimal base =3D 10.
>
> /* and the inverse transformation: string --> integer */
> printf("\n %d",atoi(cad)); // cad[ ] is transformed to integer
>
> printf("\n\n");
> system("PAUSE");
> return 0;
> }
>
> *************************************
>
>
>
> -------------------------------------------------------
> This SF.net email is sponsored by: ValueWeb:
> Dedicated Hosting for just $79/mo with 500 GB of bandwidth!
> No other company gives more support or power for your dedicated server
> http://click.atdmt.com/AFF/go/sdnxxaff00300020aff/direct/01/
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|
|
From: Ken B. <ke...@al...> - 2003-04-01 14:31:51
|
Perhaps you mean LTOA? As in:
char * ltoa ( long value, char * buffer, int radix );
Ken Bradford
Alpha II Service, Inc.
> -----Original Message-----
> From: dev...@li...
> [mailto:dev...@li...]On Behalf Of Ricardo
> L=F3pez Ruiz
> Sent: Tuesday, April 01, 2003 3:50 AM
> To: Khalid El-Kary; Dev-C++
> Subject: Re: [Dev-C++] integer to string
>
>
> perhaps this example can help you a little more.
> Ricardo.
>
>
> **********ITOA/ATOI **********
>
> #include <stdio.h>
> #include <stdlib.h>
>
> int main()
> {
> char cad[20];
>
> /* the transformation: integer --> string */
> itoa(2003,cad,10); // 2003 is the number to store in cad[]
> printf(cad); // in decimal base =3D 10.
>
> /* and the inverse transformation: string --> integer */
> printf("\n %d",atoi(cad)); // cad[ ] is transformed to integer
>
> printf("\n\n");
> system("PAUSE");
> return 0;
> }
>
> *************************************
>
>
>
> -------------------------------------------------------
> This SF.net email is sponsored by: ValueWeb:
> Dedicated Hosting for just $79/mo with 500 GB of bandwidth!
> No other company gives more support or power for your dedicated server
> http://click.atdmt.com/AFF/go/sdnxxaff00300020aff/direct/01/
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|
|
From: Ricardo R. <ri...@po...> - 2003-04-01 15:53:22
|
perhaps both functions, itoa( ) and ltoa( ), are equivalent in our case (Dev-cpp), because the "int" variables are "long int" by default (4 bytes). Ricardo. |
|
From: Per W. <pw...@ia...> - 2003-04-01 16:34:48
|
They are equivalent in one more way - they are not standard. /Per W On Tue, 1 Apr 2003, Ricardo L=F3pez Ruiz wrote: > perhaps both functions, itoa( ) and ltoa( ), are equivalent > in our case (Dev-cpp), because the "int" variables are > "long int" by default (4 bytes). > > Ricardo. > > > > ------------------------------------------------------- > This SF.net email is sponsored by: ValueWeb: > Dedicated Hosting for just $79/mo with 500 GB of bandwidth! > No other company gives more support or power for your dedicated server > http://click.atdmt.com/AFF/go/sdnxxaff00300020aff/direct/01/ > _______________________________________________ > Dev-cpp-users mailing list > Dev...@li... > TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > |
|
From: Ken B. <ke...@al...> - 2003-04-01 16:43:34
|
Ah, my mistake. I see that now; didn't look far enough. Ken Bradford Alpha II Service, Inc. > -----Original Message----- > From: Per Westermark [mailto:pw...@ia...] > Sent: Tuesday, April 01, 2003 12:33 PM > To: Ricardo L=F3pez Ruiz > Cc: ke...@al...; Dev-C++ > Subject: Re: [Dev-C++] integer to string > > > They are equivalent in one more way - they are not standard. > > /Per W > > On Tue, 1 Apr 2003, Ricardo L=F3pez Ruiz wrote: > > > perhaps both functions, itoa( ) and ltoa( ), are equivalent > > in our case (Dev-cpp), because the "int" variables are > > "long int" by default (4 bytes). > > > > Ricardo. > > > > > > > > ------------------------------------------------------- > > This SF.net email is sponsored by: ValueWeb: > > Dedicated Hosting for just $79/mo with 500 GB of bandwidth! > > No other company gives more support or power for your dedicated serve= r > > http://click.atdmt.com/AFF/go/sdnxxaff00300020aff/direct/01/ > > _______________________________________________ > > Dev-cpp-users mailing list > > Dev...@li... > > TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm > > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > > > |
|
From: Richard C. <tel...@ya...> - 2003-04-01 11:14:35
|
was just wondering if theres such thing as typecasting in C. i'm reasonably well versed in C++, dont know much C tho, so yeah, there's a suggestion. 1/04/03 12:42:48 PM, "Khalid El-Kary" <kha...@ho...> wrote: >Hi fellows, > >I'm wondering if there's a possible way to turn an integer to its string >representation in C > >//I want to get a string "5" out of f >int f=5; > >thanx in advance. > >Regards, >Khalid Al-Kary |