Up |
|
|
  |
Author: gareth.p.daviesgareth.p.davies Date: Aug 13, 2006 03:44
Hi.
Just as a learning exercise, I've come up with an implementation of
itoa() without using something like sprintf. However, I can't figure
out how to extend it to deal with negative numbers - any help
appreciated!
char * itoa(int a)
{
#define BUFFSIZE 50
static char str[BUFFSIZE];
int i = sizeof(str) - 1;
do
{
str[--i] = '0' + a % 10;
}
while ((a = a / 10) && i >= 0);
return &str[i];
}
Couple of notes on this.
|
| Show full article (1.36Kb) |
|
| | 45 Comments |
|
  |
Author: petepete Date: Aug 13, 2006 04:38
>
> Hi.
>
> Just as a learning exercise, I've come up with an implementation of
> itoa() without using something like sprintf. However, I can't figure
> out how to extend it to deal with negative numbers - any help
> appreciated!
>
> char * itoa(int a)
> {
> #define BUFFSIZE 50
>
> static char str[BUFFSIZE];
>
> int i = sizeof(str) - 1;
>
> do
> {
> str[--i] = '0' + a % 10; ...
|
| Show full article (1.53Kb) |
|
| | 4 Comments |
|
  |
Author: MQMQ Date: Aug 13, 2006 04:59
> Hi.
>
> Just as a learning exercise, I've come up with an implementation of
> itoa() without using something like sprintf. However, I can't figure
> out how to extend it to deal with negative numbers - any help
> appreciated!
>
> char * itoa(int a)
> {
> #define BUFFSIZE 50
>
> static char str[BUFFSIZE];
>
> int i = sizeof(str) - 1;
>
> do
> {
> str[--i] = '0' + a % 10;
> } ...
|
| Show full article (2.11Kb) |
| 9 Comments |
|
  |
Author: petepete Date: Aug 13, 2006 05:23
MQ wrote:
>> Hi.
>>
>> Just as a learning exercise, I've come up with an implementation of
>> itoa() without using something like sprintf. However, I can't figure
>> out how to extend it to deal with negative numbers - any help
>> appreciated!
>>
>> char * itoa(int a)
>> {
>> #define BUFFSIZE 50
>>
>> static char str[BUFFSIZE];
>>
>> int i = sizeof(str) - 1;
>>
>> do
>> { ...
|
| Show full article (1.97Kb) |
| 5 Comments |
|
  |
Author: Harald van DijkHarald van Dijk Date: Aug 13, 2006 05:50
pete wrote:
> MQ wrote:
>>> Hi.
>>>
>>> Just as a learning exercise, I've come up with an implementation of
>>> itoa() without using something like sprintf. However, I can't figure
>>> out how to extend it to deal with negative numbers - any help
>>> appreciated!
>>>
>>> char * itoa(int a)
>>> {
>>> #define BUFFSIZE 50
>>>
>>> static char str[BUFFSIZE];
>>>
>>> int i = sizeof(str) - 1;
>>>
>>> do ...
|
| Show full article (2.30Kb) |
| 4 Comments |
|
  |
Author: pemopemo Date: Aug 13, 2006 06:05
MQ wrote:
>> Hi.
>>
>> Just as a learning exercise, I've come up with an implementation of
>> itoa() without using something like sprintf. However, I can't figure
>> out how to extend it to deal with negative numbers - any help
>> appreciated!
>>
>> char * itoa(int a)
>> {
>> #define BUFFSIZE 50
>>
>> static char str[BUFFSIZE];
>>
>> int i = sizeof(str) - 1;
>>
>> do
>> {
>> str[--i] = '0' + a % 10; ...
|
| Show full article (2.74Kb) |
| no comments |
|
  |
Author: MQMQ Date: Aug 13, 2006 06:14
pete wrote:
> However, you can copy the contents of the array
> to a distinct somewhere else,
> each time that the function is called.
sounds like a poor design to me. Simply passing the char array as a
parameter would be neater. This pattern exists for such library
functions as fread()
MQ
|
| |
| no comments |
|
  |
Author: gareth.p.daviesgareth.p.davies Date: Aug 13, 2006 06:20
pete wrote:
>>
>> Hi.
>>
>> Just as a learning exercise, I've come up with an implementation of
>> itoa() without using something like sprintf. However, I can't figure
>> out how to extend it to deal with negative numbers - any help
>> appreciated!
>>
>> char * itoa(int a)
>> {
>> #define BUFFSIZE 50
>>
>> static char str[BUFFSIZE];
>>
>> int i = sizeof(str) - 1;
>>
>> do
>> { ...
|
| Show full article (2.23Kb) |
| 13 Comments |
|
  |
Author: pemopemo Date: Aug 13, 2006 06:23
MQ wrote:
>> Hi.
>>
>> Just as a learning exercise, I've come up with an implementation of
>> itoa() without using something like sprintf. However, I can't figure
>> out how to extend it to deal with negative numbers - any help
>> appreciated!
>>
>> char * itoa(int a)
>> {
>> #define BUFFSIZE 50
>>
>> static char str[BUFFSIZE];
>>
>> int i = sizeof(str) - 1;
>>
>> do
>> {
>> str[--i] = '0' + a % 10; ...
|
| Show full article (2.35Kb) |
| no comments |
|
  |
|
|
  |
Author: Ancient_HackerAncient_Hacker Date: Aug 13, 2006 06:47
Here's one that handles negative numbers:
#define BUFFSIZE 30
#define pc(c) ( 12345 == (*p99++ = c))
char * p99; char s99[BUFFSIZE];
void putd( int aa ){ if( aa > 9 ) putd( aa / 10 ); pc( (char) (aa %
10) + '0' ); }
char * ita( int a ) { p99 = s99; putd( a < 0 ? pc( '-' ) - a : a
);return s99 + pc( '\0' );
}
int main( int argc, char * argv[] )
{ char foo[1000]; int i; char * s; );
for( i = -123; i < 123 ; i ++ ) { s = ita( i ); printf( "%d = '%s'\n",
i, s ); }
gets_s( foo );
return 0;
}
|
| |
| no comments |
|
|
|