| 1 | /* $Id: kHlpInt2Ascii.c 3573 2007-08-31 04:09:23Z bird $ */
|
|---|
| 2 | /** @file
|
|---|
| 3 | * kHlpString - kHlpInt2Ascii.
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | /*
|
|---|
| 7 | * Copyright (c) 2006-2007 knut st. osmundsen <bird-src-spam@anduin.net>
|
|---|
| 8 | *
|
|---|
| 9 | * This file is part of kStuff.
|
|---|
| 10 | *
|
|---|
| 11 | * kStuff is free software; you can redistribute it and/or
|
|---|
| 12 | * modify it under the terms of the GNU Lesser General Public
|
|---|
| 13 | * License as published by the Free Software Foundation; either
|
|---|
| 14 | * version 2.1 of the License, or (at your option) any later version.
|
|---|
| 15 | *
|
|---|
| 16 | * kStuff is distributed in the hope that it will be useful,
|
|---|
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|---|
| 19 | * Lesser General Public License for more details.
|
|---|
| 20 | *
|
|---|
| 21 | * You should have received a copy of the GNU Lesser General Public
|
|---|
| 22 | * License along with kStuff; if not, write to the Free Software
|
|---|
| 23 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|---|
| 24 | *
|
|---|
| 25 | */
|
|---|
| 26 |
|
|---|
| 27 | /*******************************************************************************
|
|---|
| 28 | * Header Files *
|
|---|
| 29 | *******************************************************************************/
|
|---|
| 30 | #include <k/kHlpString.h>
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 | /**
|
|---|
| 34 | * Converts an signed integer to an ascii string.
|
|---|
| 35 | *
|
|---|
| 36 | * @returns psz.
|
|---|
| 37 | * @param psz Pointer to the output buffer.
|
|---|
| 38 | * @param cch The size of the output buffer.
|
|---|
| 39 | * @param lVal The value.
|
|---|
| 40 | * @param iBase The base to format it. (2,8,10 or 16)
|
|---|
| 41 | */
|
|---|
| 42 | KHLP_DECL(char *) kHlpInt2Ascii(char *psz, KSIZE cch, long lVal, unsigned iBase)
|
|---|
| 43 | {
|
|---|
| 44 | static const char s_szDigits[] = "0123456789abcdefghijklmnopqrstuvwxyz";
|
|---|
| 45 | char *pszRet = psz;
|
|---|
| 46 |
|
|---|
| 47 | if (cch >= (lVal < 0 ? 3U : 2U) && psz)
|
|---|
| 48 | {
|
|---|
| 49 | /* prefix */
|
|---|
| 50 | if (lVal < 0)
|
|---|
| 51 | {
|
|---|
| 52 | *psz++ = '-';
|
|---|
| 53 | cch--;
|
|---|
| 54 | lVal = -lVal;
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | /* the digits */
|
|---|
| 58 | do
|
|---|
| 59 | {
|
|---|
| 60 | *psz++ = s_szDigits[lVal % iBase];
|
|---|
| 61 | cch--;
|
|---|
| 62 | lVal /= iBase;
|
|---|
| 63 | } while (lVal && cch > 1);
|
|---|
| 64 |
|
|---|
| 65 | /* overflow indicator */
|
|---|
| 66 | if (lVal)
|
|---|
| 67 | psz[-1] = '+';
|
|---|
| 68 | }
|
|---|
| 69 | else if (!pszRet)
|
|---|
| 70 | return pszRet;
|
|---|
| 71 | else if (cch < 1 || !pszRet)
|
|---|
| 72 | return pszRet;
|
|---|
| 73 | else
|
|---|
| 74 | *psz++ = '+';
|
|---|
| 75 | *psz = '\0';
|
|---|
| 76 |
|
|---|
| 77 | return pszRet;
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|