How to write the code to realize itoa?

C

cai_rongxi

I know how to write c code to realize atoi function, but I don't know
how to do the opposit. Any sample code is appreciated.
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I know how to write c code to realize atoi function, but I don't know
how to do the opposit. Any sample code is appreciated.

You can check the archives of this newsgroup for a number of solutions. One
that I contributed long ago can be found at
http://groups.google.com/groups?hl=en&lr=&[email protected]

- --
Lew Pitcher

Master Codewright & JOAT-in-training | GPG public key available on request
Registered Linux User #112576 (http://counter.li.org/)
Slackware - Because I know what I'm doing.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFBpAdKagVFX4UWr64RAq9bAKCpABdkOOhh3OCtXE54OLunXCwCOgCg5Fcw
+CFp4sh/zmzAhCR6c1hbw0k=
=NELE
-----END PGP SIGNATURE-----
 
E

Erik de Castro Lopo

I know how to write c code to realize atoi function, but I don't know
how to do the opposit. Any sample code is appreciated.


Without any error checking:


char * itoa (int i)
{
char str_val [32] ;
snprintf (str_val, sizeof (str_val), "%d", i) ;

/* Use strdup to duplicate str_val which is currently on the stack. */
return strdup (str_val) ;
}



--
+-----------------------------------------------------------+
Erik de Castro Lopo (e-mail address removed) (Yes it's valid)
+-----------------------------------------------------------+
`[Microsoft] are in the business of giving customers exactly what they ask
for, which sounds like a nice idea until you realize that most Microsoft
customers are idiots.' --- Eugene O'Neil on comp.os.linux.development.system
 
J

jt

I know how to write c code to realize atoi function, but I don't know
how to do the opposit. Any sample code is appreciated.

/* atoi example */
#include <stdio.h>
#include <stdlib.h>

int main ()
{
int i;
char szInput [256];
printf ("Enter a number: ");
gets ( szInput );
i = atoi (szInput);
printf ("Value entered is %d, and its double %d",i,i*2);
return 0;
}
Output:
Enter a number: 73
Value entered is 73, and its double 146
 
C

Chris Barts

jt said:
gets ( szInput );

If your compiler or linker doesn't throw up a warning about this line,
your tools are broken and need to be replaced for the good of everyone
who uses code you compile.

Besides, your code doesn't help him do anything about writing an itoa
function.
 
S

suman kar

I know how to write c code to realize atoi function, but I don't know
how to do the opposit. Any sample code is appreciated.

A quick and dirty hack would be using sprintf() function, which you can
find in stdio.h, a standard library.Don't ask for code for this one :)

Regards,
Suman.
 
S

Souvik

I know how to write c code to realize atoi function, but I don't know
how to do the opposit. Any sample code is appreciated.

Here's one:

#include "stdio.h"
#include "string.h"

void itochar(int x, char *szBuffer, int radix);


int main()
{
char szBuffer[10];

itochar(725, szBuffer, 10);

printf ("%s", szBuffer);
return 0;
}

void itochar(int x, char *szBuffer, int radix)
{
int i = 0 , n,xx;
n = x;
while (n > 0)
{
xx = n%radix;
n = n/radix;
szBuffer[i++] = '0' + xx;
}
szBuffer = '\0';
strrev(szBuffer);
}
 
M

Michael Mair

Souvik said:
I know how to write c code to realize atoi function, but I don't know
how to do the opposit. Any sample code is appreciated.


Here's one:

#include "stdio.h"
#include "string.h"

void itochar(int x, char *szBuffer, int radix);


int main()
{
char szBuffer[10];

itochar(725, szBuffer, 10);
What will you do for base 2? Or log(INT_MAX)/log(10)>10?
It is a Good Idea to pass the buffer size, too.
printf ("%s", szBuffer);
A newline character is required if you want to have that output;
printf ("%s", szBuffer);
return 0;
}

void itochar(int x, char *szBuffer, int radix)
{
int i = 0 , n,xx;
n = x;
while (n > 0)
{
xx = n%radix;
n = n/radix;
szBuffer[i++] = '0' + xx;
Nice, but will not work for radices > 10. Use a string
with your symbols instead, e.g.
szBuffer[i++] = "0123456789abcdefghijklmnopqrstuvwxyz"[xx];
Apart from that, your code does not work with negative numbers.
}
szBuffer = '\0';
strrev(szBuffer);

They must have left that one out of my standard; at least
there is nothing between strrchr() and strspn()...
Functions of your own should not start with str in order to
not clash with future library extensions.

BTW: itochar could be written in a more compact way without use of
i,n,xx.


Cheers
Michael
 
M

Malcolm

I know how to write c code to realize atoi function, but I don't know
how to do the opposit. Any sample code is appreciated.
Take modulus ten, convert to a digit. The divide by ten. Take modulus ten to
get the next digit, and so on until your number reaches zero. Then reverse
the digits.
You can easily extend to negative numbers by adding a minus sign and
negating, unless you are passed INT_MIN. This probably won't matter.
 
P

pete

I know how to write c code to realize atoi function, but I don't know
how to do the opposit. Any sample code is appreciated.

#include <limits.h>
void itoa_1(int n, char *s)
{
int tenth, min_flag;
char swap, *p;

min_flag = 0;
if (0 > n) {
*s++ = '-';
n = -INT_MAX > n ? min_flag = INT_MAX : -n;
}
p = s;
do {
tenth = n / 10;
*p++ = (char)(n - 10 * tenth + '0');
n = tenth;
} while (n != 0);
if (min_flag != 0) {
++*s;
}
*p-- = '\0';
while (p > s) {
swap = *s;
*s++ = *p;
*p-- = swap;
}
}

static char *sput_u(unsigned n, char *s);
static char *sput_up1(unsigned n, char *s);
void itoa_2(int n, char *s)
{
if (0 > n) {
*s++ = '-';
*sput_up1(-(n + 1), s) = '\0';
} else {
*sput_u(n, s) = '\0';
}
}

static char *sput_u(unsigned n, char *s)
{
unsigned tenth;

tenth = n / 10;
if (tenth != 0) {
s = sput_u(tenth, s);
}
*s = (char)(n - 10 * tenth + '0');
return s + 1;
}

static char *sput_up1(unsigned n, char *s)
{
unsigned digit, tenth;

tenth = n / 10;
digit = n - 10 * tenth;
if (digit == 9) {
if (tenth != 0) {
s = sput_up1(tenth, s);
} else {
*s++ = '1';
}
*s = '0';
} else {
if (tenth != 0) {
s = sput_u(tenth, s);
}
*s = (char)('1' + digit);
}
return s + 1;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top