From 7e0f021a9aec35fd8e6725e87e3313b101d26f5e Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Sun, 27 Jan 2008 11:37:44 +0100 Subject: Initial import (2.0.2-6) --- reference/C/CONTRIB/SNIP/ltoa.c | 58 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100755 reference/C/CONTRIB/SNIP/ltoa.c (limited to 'reference/C/CONTRIB/SNIP/ltoa.c') diff --git a/reference/C/CONTRIB/SNIP/ltoa.c b/reference/C/CONTRIB/SNIP/ltoa.c new file mode 100755 index 0000000..a241e7b --- /dev/null +++ b/reference/C/CONTRIB/SNIP/ltoa.c @@ -0,0 +1,58 @@ +/* +** LTOA.C +** +** Converts a long integer to a string. +** +** Copyright 1988-90 by Robert B. Stout dba MicroFirm +** +** Released to public domain, 1991 +** +** Parameters: 1 - number to be converted +** 2 - buffer in which to build the converted string +** 3 - number base to use for conversion +** +** Returns: A character pointer to the converted string if +** successful, a NULL pointer if the number base specified +** is out of range. +*/ + +#include +#include + +#define BUFSIZE (sizeof(long) * 8 + 1) + +char *ltoa(long N, char *str, int base) +{ + register int i = 2; + long uarg; + char *tail, *head = str, buf[BUFSIZE]; + + if (36 < base || 2 > base) + base = 10; /* can only use 0-9, A-Z */ + tail = &buf[BUFSIZE - 1]; /* last character position */ + *tail-- = '\0'; + + if (10 == base && N < 0L) + { + *head++ = '-'; + uarg = -N; + } + else uarg = N; + + if (uarg) + { + for (i = 1; uarg; ++i) + { + register ldiv_t r; + + r = ldiv(uarg, base); + *tail-- = (char)(r.rem + ((9L < r.rem) ? + ('A' - 10L) : '0')); + uarg = r.quot; + } + } + else *tail-- = '0'; + + memcpy(head, ++tail, i); + return str; +} -- cgit v1.2.3-54-g00ecf