Portál AbcLinuxu, 8. listopadu 2025 08:24
itoa() opravdu neexistuje...
atoi()), ale ne v POSIXu ani v dalších rozšířeních glibc; tipoval bych, že jde o pokus přeložit program psaný původně pro nějakou jinou platformu. Takhle to dopadá, když se nepoužívá -Wall nebo se ignorují varování překladače.
#include <stdio.h>
#include <string.h>
/*
Converts an integer value to a null-terminated string using
the specified radix and stores the result in the given buffer.
If radix is 10 and value is negative the string is preceded by
the minus sign (-). With any other radix, value is always considered
unsigned. buffer should be large enough to contain any possible value:
(sizeof(int)*8+1) for radix=2, i.e. 17 bytes in 16-bits platforms
and 33 in 32-bits platforms.
Parameters:
value Value to be represented as a string.
buffer Buffer where to store the resulting string.
radix Numeral radix in which value has to be represented, between 2 and 36.
Return Value:
A pointer to the string.
No error checking is performed.
*/
char* itoa (int value, char* buffer, int radix)
{
if ( radix == 10 ) {
sprintf (buffer, "%d", value);
} else {
char tmp_buffer[ sizeof(int)*8 + 1 ];
char *ptr = &tmp_buffer[ sizeof(int)*8 ];
unsigned n = value;
*ptr-- = 0;
do {
*ptr-- = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" [n % radix];
n /= radix;
} while ( n );
strcpy (buffer, ++ptr);
}
}
int main ()
{
int value, radix;
char buffer[sizeof(int)*8 + 1];
while (1) {
printf ("Value (decimal): "); scanf ("%d", &value);
printf ("Radix (2 .. 36): "); scanf ("%d", &radix);
if ( radix < 2 || radix > 36 ) {
printf("Good bye!\n");
return 0;
}
itoa (value, buffer, radix);
printf("Result: %s\n", buffer);
}
}
#include <stdio.h>
#include <string.h>
/*
Converts an integer value to a null-terminated string using
the specified radix and stores the result in the given buffer.
If radix is 10 and value is negative the string is preceded by
the minus sign (-). With any other radix, value is always considered
unsigned. buffer should be large enough to contain any possible value:
(sizeof(int)*8+1) for radix=2, i.e. 17 bytes in 16-bits platforms
and 33 in 32-bits platforms.
Parameters:
value Value to be represented as a string.
buffer Buffer where to store the resulting string.
radix Numeral radix in which value has to be represented, between 2 and 36.
Return Value:
A pointer to the string.
No error checking is performed.
*/
char* itoa (int value, char* buffer, int radix)
{
if ( radix == 10 ) {
sprintf (buffer, "%d", value);
} else {
char tmp_buffer[ sizeof(int)*8 + 1 ];
char *ptr = &tmp_buffer[ sizeof(int)*8 ];
unsigned n = value;
*ptr-- = 0;
do {
*ptr-- = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" [n % radix];
n /= radix;
} while ( n );
strcpy (buffer, ++ptr);
}
}
int main ()
{
int value, radix;
char buffer[sizeof(int)*8 + 1];
while (1) {
printf ("Value (decimal): "); scanf ("%d", &value);
printf ("Radix (2 .. 36): "); scanf ("%d", &radix);
if ( radix < 2 || radix > 36 ) {
printf("Good bye!\n");
return 0;
}
itoa (value, buffer, radix);
printf("Result: %s\n", buffer);
}
}
return buffer;. Takze nejak takto:
} while ( n );
strcpy (buffer, ++ptr);
}
return buffer;
}
#include <stdlib.h>
a pak už jen
itoa(I,S,i)
kde I je vstupní integer; S výstupní string a i je soustava (2-dvojkova;8-osmičková;10-desítková;16-hexadecimální)
$ grep itoa /usr/include/gentoo-multilib/amd64/stdlib.h $?
Tiskni
Sdílej:
ISSN 1214-1267, (c) 1999-2007 Stickfish s.r.o.