1 | /* $Id: kHlpInt2Ascii.c 2 2007-11-16 16:07:14Z bird $ */
|
---|
2 | /** @file
|
---|
3 | * kHlpString - kHlpInt2Ascii.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (c) 2006-2007 knut st. osmundsen <[email protected]>
|
---|
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 | * In addition to the permissions in the GNU Lesser General Public
|
---|
17 | * License, you are granted unlimited permission to link the compiled
|
---|
18 | * version of this file into combinations with other programs, and to
|
---|
19 | * distribute those combinations without any restriction coming from
|
---|
20 | * the use of this file.
|
---|
21 | *
|
---|
22 | * kStuff is distributed in the hope that it will be useful,
|
---|
23 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
24 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
25 | * Lesser General Public License for more details.
|
---|
26 | *
|
---|
27 | * You should have received a copy of the GNU Lesser General Public
|
---|
28 | * License along with kStuff; if not, write to the Free Software
|
---|
29 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
---|
30 | * 02110-1301, USA
|
---|
31 | */
|
---|
32 |
|
---|
33 | /*******************************************************************************
|
---|
34 | * Header Files *
|
---|
35 | *******************************************************************************/
|
---|
36 | #include <k/kHlpString.h>
|
---|
37 |
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Converts an signed integer to an ascii string.
|
---|
41 | *
|
---|
42 | * @returns psz.
|
---|
43 | * @param psz Pointer to the output buffer.
|
---|
44 | * @param cch The size of the output buffer.
|
---|
45 | * @param lVal The value.
|
---|
46 | * @param iBase The base to format it. (2,8,10 or 16)
|
---|
47 | */
|
---|
48 | KHLP_DECL(char *) kHlpInt2Ascii(char *psz, KSIZE cch, long lVal, unsigned iBase)
|
---|
49 | {
|
---|
50 | static const char s_szDigits[] = "0123456789abcdefghijklmnopqrstuvwxyz";
|
---|
51 | char *pszRet = psz;
|
---|
52 |
|
---|
53 | if (cch >= (lVal < 0 ? 3U : 2U) && psz)
|
---|
54 | {
|
---|
55 | /* prefix */
|
---|
56 | if (lVal < 0)
|
---|
57 | {
|
---|
58 | *psz++ = '-';
|
---|
59 | cch--;
|
---|
60 | lVal = -lVal;
|
---|
61 | }
|
---|
62 |
|
---|
63 | /* the digits */
|
---|
64 | do
|
---|
65 | {
|
---|
66 | *psz++ = s_szDigits[lVal % iBase];
|
---|
67 | cch--;
|
---|
68 | lVal /= iBase;
|
---|
69 | } while (lVal && cch > 1);
|
---|
70 |
|
---|
71 | /* overflow indicator */
|
---|
72 | if (lVal)
|
---|
73 | psz[-1] = '+';
|
---|
74 | }
|
---|
75 | else if (!pszRet)
|
---|
76 | return pszRet;
|
---|
77 | else if (cch < 1 || !pszRet)
|
---|
78 | return pszRet;
|
---|
79 | else
|
---|
80 | *psz++ = '+';
|
---|
81 | *psz = '\0';
|
---|
82 |
|
---|
83 | return pszRet;
|
---|
84 | }
|
---|
85 |
|
---|