Last change
on this file since 577 was
444,
checked in by satin@…, 6 years ago
|
add newlib,libalmos-mkh, restructure shared_syscalls.h and mini-libc
|
File size:
1.4 KB
|
Line | |
---|
1 | /* |
---|
2 | FUNCTION |
---|
3 | <<utoa>>---unsigned integer to string |
---|
4 | |
---|
5 | INDEX |
---|
6 | utoa |
---|
7 | |
---|
8 | SYNOPSIS |
---|
9 | #include <stdlib.h> |
---|
10 | char *utoa(unsigned <[value]>, char *<[str]>, int <[base]>); |
---|
11 | char *__utoa(unsigned <[value]>, char *<[str]>, int <[base]>); |
---|
12 | |
---|
13 | DESCRIPTION |
---|
14 | <<utoa>> converts the unsigned integer [<value>] to a null-terminated string |
---|
15 | using the specified base, which must be between 2 and 36, inclusive. |
---|
16 | <[str]> should be an array long enough to contain the converted |
---|
17 | value, which in the worst case is sizeof(int)*8+1 bytes. |
---|
18 | |
---|
19 | RETURNS |
---|
20 | A pointer to the string, <[str]>, or NULL if <[base]> is invalid. |
---|
21 | |
---|
22 | PORTABILITY |
---|
23 | <<utoa>> is non-ANSI. |
---|
24 | |
---|
25 | No supporting OS subroutine calls are required. |
---|
26 | */ |
---|
27 | |
---|
28 | #include <stdlib.h> |
---|
29 | |
---|
30 | char * |
---|
31 | __utoa (unsigned value, |
---|
32 | char *str, |
---|
33 | int base) |
---|
34 | { |
---|
35 | const char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz"; |
---|
36 | int i, j; |
---|
37 | unsigned remainder; |
---|
38 | char c; |
---|
39 | |
---|
40 | /* Check base is supported. */ |
---|
41 | if ((base < 2) || (base > 36)) |
---|
42 | { |
---|
43 | str[0] = '\0'; |
---|
44 | return NULL; |
---|
45 | } |
---|
46 | |
---|
47 | /* Convert to string. Digits are in reverse order. */ |
---|
48 | i = 0; |
---|
49 | do |
---|
50 | { |
---|
51 | remainder = value % base; |
---|
52 | str[i++] = digits[remainder]; |
---|
53 | value = value / base; |
---|
54 | } while (value != 0); |
---|
55 | str[i] = '\0'; |
---|
56 | |
---|
57 | /* Reverse string. */ |
---|
58 | for (j = 0, i--; j < i; j++, i--) |
---|
59 | { |
---|
60 | c = str[j]; |
---|
61 | str[j] = str[i]; |
---|
62 | str[i] = c; |
---|
63 | } |
---|
64 | |
---|
65 | return str; |
---|
66 | } |
---|
67 | |
---|
68 | char * |
---|
69 | utoa (unsigned value, |
---|
70 | char *str, |
---|
71 | int base) |
---|
72 | { |
---|
73 | return __utoa (value, str, base); |
---|
74 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.