473,408 Members | 2,477 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

one c question

how to use itoa() with example

Nov 14 '05 #1
7 1505
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
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
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
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
-----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
"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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Mohammed Mazid | last post by:
Can anyone please help me on how to move to the next and previous question? Here is a snippet of my code: Private Sub cmdNext_Click() End Sub Private Sub cmdPrevious_Click() showrecord
3
by: Stevey | last post by:
I have the following XML file... <?xml version="1.0"?> <animals> <animal> <name>Tiger</name> <questions> <question index="0">true</question> <question index="1">true</question> </questions>
7
by: nospam | last post by:
Ok, 3rd or is it the 4th time I have asked this question on Partial Types, so, since it seems to me that Partial Types is still in the design or development stages at Microsoft, I am going to ask...
3
by: Ekqvist Marko | last post by:
Hi, I have one Access database table including questions and answers. Now I need to give answer id automatically to questionID column. But I don't know how it is best (fastest) to do? table...
10
by: glenn | last post by:
I am use to programming in php and the way session and post vars are past from fields on one page through to the post page automatically where I can get to their values easily to write to a...
10
by: Rider | last post by:
Hi, simple(?) question about asp.net configuration.. I've installed ASP.NET 2.0 QuickStart Sample successfully. But, When I'm first start application the follow message shown. ========= Server...
53
by: Jeff | last post by:
In the function below, can size ever be 0 (zero)? char *clc_strdup(const char * CLC_RESTRICT s) { size_t size; char *p; clc_assert_not_null(clc_strdup, s); size = strlen(s) + 1;
56
by: spibou | last post by:
In the statement "a *= expression" is expression assumed to be parenthesized ? For example if I write "a *= b+c" is this the same as "a = a * (b+c)" or "a = a * b+c" ?
2
by: Allan Ebdrup | last post by:
Hi, I'm trying to render a Matrix question in my ASP.Net 2.0 page, A matrix question is a question where you have several options that can all be rated according to several possible ratings (from...
3
by: Zhang Weiwu | last post by:
Hello! I wrote this: ..required-question p:after { content: "*"; } Corresponding HTML: <div class="required-question"><p>Question Text</p><input /></div> <div...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.