one c question

  • Thread starter Nayak Abinash XN (AS/EAB)
  • Start date
R

Ramasubramanian XR (AS/EAB)

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;
}
 
R

Ramasubramanian XR (AS/EAB)

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;
}
 
P

pete

Nayak said:
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 */
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
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&lr=&[email protected]
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)

iD8DBQFCUVczagVFX4UWr64RAub0AJ9bqkbgV8opu30wRw5jjAzS4CmuXwCglJWW
DUtL6vc2hXJLyWlxi1ekxTM=
=N3JB
-----END PGP SIGNATURE-----
 
O

osmium

Nayak Abinash XN (AS/EAB) said:
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.
 
E

Erik de Castro Lopo

Ramasubramanian XR (AS/EAB) said:
#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 (e-mail address removed) (Yes it's valid)
+-----------------------------------------------------------+
"There is no reason why anyone would want a computer in their home"
Ken Olson, DEC, 1977
 

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,780
Messages
2,569,611
Members
45,282
Latest member
RoseannaBa

Latest Threads

Top