source: trunk/libs/newlib/src/newlib/libc/stdlib/l64a.c @ 567

Last change on this file since 567 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/* l64a - convert long to radix-64 ascii string
2 *         
3 * Conversion is performed on at most 32-bits of input value starting
4 * from least significant bits to the most significant bits.
5 *
6 * The routine splits the input value into groups of 6 bits for up to
7 * 32 bits of input.  This means that the last group may be 2 bits
8 * (bits 30 and 31).
9 *
10 * Each group of 6 bits forms a value from 0-63 which is converted into
11 * a character as follows:
12 *         0 = '.'
13 *         1 = '/'
14 *         2-11 = '0' to '9'
15 *        12-37 = 'A' to 'Z'
16 *        38-63 = 'a' to 'z'
17 *
18 * When the remaining bits are zero or all 32 bits have been translated,
19 * a nul terminator is appended to the resulting string.  An input value of
20 * 0 results in an empty string.
21 */
22
23#include <_ansi.h>
24#include <stdlib.h>
25#include <reent.h>
26
27static const char R64_ARRAY[] = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
28
29char *
30l64a (long value)
31{
32  return _l64a_r (_REENT, value);
33}
34
35char *
36_l64a_r (struct _reent *rptr,
37     long value)
38{
39  char *ptr;
40  char *result;
41  int i, index;
42  unsigned long tmp = (unsigned long)value & 0xffffffff;
43
44  _REENT_CHECK_MISC(rptr);
45  result = _REENT_L64A_BUF(rptr);
46  ptr = result;
47
48  for (i = 0; i < 6; ++i)
49    {
50      if (tmp == 0)
51        {
52          *ptr = '\0';
53          break;
54        }
55
56      index = tmp & (64 - 1);
57      *ptr++ = R64_ARRAY[index];
58      tmp >>= 6;
59    }
60
61  return result;
62}
Note: See TracBrowser for help on using the repository browser.