1 /*
2 * Copyright (C) 1998,1999 Uwe Ohse
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 *
18 * As a special exception this source may be used as part of the
19 * SRS project by CORE/Computer Service Langenbach
20 * regardless of the copyright they choose.
21 *
22 * Contact: uwe@ohse.de
23 */
24 #include "str_ulong.h"
25
26 size_t
str_ulong_base(char * s,unsigned long u,unsigned int base)27 str_ulong_base(char *s, unsigned long u, unsigned int base)
28 {
29 const char *b="0123456789abcdefghijklmnopqrstuvwxyz";
30 unsigned int len=1;
31 unsigned long tmp=u;
32 char *end;
33 while (tmp>=base) {
34 len++;
35 tmp/=base;
36 }
37 if (!s)
38 return len;
39 end=s=s+len;
40 while (u>=base) {
41 s--;
42 *s=b[u%base];
43 u/=base;
44 }
45 s--;
46 *s=b[u%base];
47 *end=0;
48 return len;
49 }
50
51 #ifdef TEST
main(int argc,char ** argv)52 int main(int argc, char **argv)
53 {
54 char b[STR_ULONG];
55 size_t l;
56 l=str_ulong_base(b, strtoul(argv[1],0,0), strtoul(argv[2],0,0));
57 write(1,b,l);
58 write(1,"\n",1);
59 exit(1);
60 }
61 #endif
62