By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,105 Members | 1,090 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,105 IT Pros & Developers. It's quick & easy.

one c question

P: n/a
how to use itoa() with example

Nov 14 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
On 2005-04-03 19:49, Nayak Abinash XN (AS/EAB) wrote:
how to use itoa() with example

perform well abinash

Nov 14 '05 #2

P: n/a
On 2005-04-03 19:49, Nayak Abinash XN (AS/EAB) wrote:
how to use itoa() with example

#include <stdio.h>
#include <stdlib.h>

int main ()
{
int i;
char buffer [33];
printf ("Enter a number: ");
scanf ("%d",&i);
itoa (i,buffer,10);
printf ("decimal: %s\n",buffer);
itoa (i,buffer,16);
printf ("hexadecimal: %s\n",buffer);
itoa (i,buffer,2);
printf ("binary: %s\n",buffer);
return 0;
}

Nov 14 '05 #3

P: n/a
On 2005-04-03 19:49, Nayak Abinash XN (AS/EAB) wrote:
how to use itoa() with example

#include <crtdll/errno.h>
#include <crtdll/stdlib.h>
#include <crtdll/internal/file.h>

char *
itoa(int value, char *string, int radix)
{
char tmp[33];
char *tp = tmp;
int i;
unsigned v;
int sign;
char *sp;

if (radix > 36 || radix <= 1)
{
__set_errno(EDOM);
return 0;
}

sign = (radix == 10 && value < 0);
if (sign)
v = -value;
else
v = (unsigned)value;
while (v || tp == tmp)
{
i = v % radix;
v = v / radix;
if (i < 10)
*tp++ = i+'0';
else
*tp++ = i + 'a' - 10;
}

if (string == 0)
string = (char *)malloc((tp-tmp)+sign+1);
sp = string;

if (sign)
*sp++ = '-';
while (tp > tmp)
*sp++ = *--tp;
*sp = 0;
return string;
}
char *
ltoa(long value, char *string, int radix)
{
char tmp[33];
char *tp = tmp;
long i;
unsigned long v;
int sign;
char *sp;

if (radix > 36 || radix <= 1)
{
__set_errno(EDOM);
return 0;
}

sign = (radix == 10 && value < 0);
if (sign)
v = -value;
else
v = (unsigned long)value;
while (v || tp == tmp)
{
i = v % radix;
v = v / radix;
if (i < 10)
*tp++ = i+'0';
else
*tp++ = i + 'a' - 10;
}

if (string == 0)
string = (char *)malloc((tp-tmp)+sign+1);
sp = string;

if (sign)
*sp++ = '-';
while (tp > tmp)
*sp++ = *--tp;
*sp = 0;
return string;
}

char *
_ultoa(unsigned long value, char *string, int radix)
{
char tmp[33];
char *tp = tmp;
long i;
unsigned long v = value;
char *sp;

if (radix > 36 || radix <= 1)
{
__set_errno(EDOM);
return 0;
}
while (v || tp == tmp)
{
i = v % radix;
v = v / radix;
if (i < 10)
*tp++ = i+'0';
else
*tp++ = i + 'a' - 10;
}

if (string == 0)
string = (char *)malloc((tp-tmp)+1);
sp = string;
while (tp > tmp)
*sp++ = *--tp;
*sp = 0;
return string;
}
Nov 14 '05 #4

P: n/a
Nayak Abinash XN (AS/EAB) wrote:

how to use itoa() with example


/* BEGIN new.c */

#include <stdio.h>
#include <limits.h>

void itoa(int n, char *s);
int fsput_d(int integer);

int main(void)
{
fputs("INT_MIN is ", stdout);
fsput_d(INT_MIN);
putchar('\n');

return 0;
}

int fsput_d(int integer)
{
char string[CHAR_BIT / 2 * sizeof integer];

itoa(integer, string);
return fputs(string, stdout);
}

void itoa(int n, char *s)
{
int tenth, min_offset;
char swap, *p;

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

/* END new.c */

--
pete
Nov 14 '05 #5

P: n/a
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Nayak Abinash XN (AS/EAB) wrote:
how to use itoa() with example


IIRC, itoa() is not an ISO C function. As such, it is hard to show you how to
use itoa(), as it takes many forms and can be used in many ways.

We've had a large number of discussions around itoa(); see
http://groups.google.ca/groups?hl=en...81%40yesic.com
for one of them. That thread includes my attempt at an itoa() function :-)

FWIW, I've seen three variations on the itoa() parameter list on three different
platforms. Depending on your platform you may have one of those three
variations, or no itoa() at all. Thus, it's hard to answer your question,
because we have no idea which of the many non-standard variations of the
non-standard function you want to see an example of the use of.
- --
Lew Pitcher
IT Specialist, Enterprise Data Systems,
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed are my own, not my employers')
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFCUVczagVFX4UWr64RAub0AJ9bqkbgV8opu30wRw5jjA zS4CmuXwCglJWW
DUtL6vc2hXJLyWlxi1ekxTM=
=N3JB
-----END PGP SIGNATURE-----
Nov 14 '05 #6

P: n/a
"Nayak Abinash XN (AS/EAB)" writes:
how to use itoa() with example


That is not a standard function, but there is an atoi() function. If you
are not discussing well-known functions, it is best to describe what it is
you want to do, rather than assigning it a name of your choosing. sprintf()
in <stdio.h> does about what is suggested by the acronym you have assigned.
Nov 14 '05 #7

P: n/a
"Ramasubramanian XR (AS/EAB)" wrote:

On 2005-04-03 19:49, Nayak Abinash XN (AS/EAB) wrote:
how to use itoa() with example

#include <crtdll/errno.h>
#include <crtdll/stdlib.h>
#include <crtdll/internal/file.h>


Those are not standard C header files. This newsgroups limits itself
to standard C.
char *
itoa(int value, char *string, int radix)


????????

Why go to all the trouble of writing and maintaining an itoa
function when snprintf (which is standard C) is available in
the standard library.
char buffer [32] ;
snprintf (buffer, sizeof (buffer), "%d", value_tp_convert);

Erik
--
+-----------------------------------------------------------+
Erik de Castro Lopo no****@mega-nerd.com (Yes it's valid)
+-----------------------------------------------------------+
"There is no reason why anyone would want a computer in their home"
Ken Olson, DEC, 1977
Nov 14 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.