]> asedeno.scripts.mit.edu Git - 1ts-debian.git/blob - zephyr/lib/ZMakeAscii.c
d188536c6a81807ac6f98669de4e3ab4b4181ffc
[1ts-debian.git] / zephyr / lib / ZMakeAscii.c
1 /* This file is part of the Project Athena Zephyr Notification System.
2  * It contains source for the ZMakeAscii function.
3  *
4  *      Created by:     Robert French
5  *
6  *      $Id$
7  *
8  *      Copyright (c) 1987 by the Massachusetts Institute of Technology.
9  *      For copying and distribution information, see the file
10  *      "mit-copyright.h". 
11  */
12
13 #include <internal.h>
14 #include <assert.h>
15
16 #ifndef lint
17 static const char rcsid_ZMakeAscii_c[] = "$Id$";
18 #endif
19
20 static char *itox_chars = "0123456789ABCDEF";
21
22 Code_t ZMakeAscii(ptr, len, field, num)
23     register char *ptr;
24     int len;
25     unsigned char *field;
26     int num;
27 {
28     int i;
29
30     for (i=0;i<num;i++) {
31         /* we need to add "0x" if we are between 4 byte pieces */
32         if ((i & 3) == 0) {
33             if (len < (i?4:3))
34                 return ZERR_FIELDLEN;
35             /* except at the beginning, put a space in before the "0x" */
36             if (i) {
37                 *ptr++ = ' ';
38                 len--;
39             }
40             *ptr++ = '0';
41             *ptr++ = 'x';
42             len -= 2;
43         } 
44         if (len < 3)
45             return ZERR_FIELDLEN;
46         *ptr++ = itox_chars[(int) (field[i] >> 4)];
47         *ptr++ = itox_chars[(int) (field[i] & 0xf)];
48         len -= 2;
49     }
50
51     *ptr = '\0';
52     return ZERR_NONE;
53 }
54
55 Code_t ZMakeAscii32(ptr, len, value)
56     register char *ptr;
57     int len;
58     unsigned long value;
59 {
60     if (len < 11)
61         return ZERR_FIELDLEN;
62     *ptr++ = '0';
63     *ptr++ = 'x';
64     *ptr++ = itox_chars[(value >> 28) & 0xf];
65     *ptr++ = itox_chars[(value >> 24) & 0xf];
66     *ptr++ = itox_chars[(value >> 20) & 0xf];
67     *ptr++ = itox_chars[(value >> 16) & 0xf];
68     *ptr++ = itox_chars[(value >> 12) & 0xf];
69     *ptr++ = itox_chars[(value >>  8) & 0xf];
70     *ptr++ = itox_chars[(value >>  4) & 0xf];
71     *ptr++ = itox_chars[(value >>  0) & 0xf];
72     *ptr = 0;
73     return ZERR_NONE;
74 }
75
76 Code_t ZMakeAscii16(ptr, len, value)
77     register char *ptr;
78     int len;
79     unsigned int value;
80 {
81     if (len < 7)
82         return ZERR_FIELDLEN;
83     *ptr++ = '0';
84     *ptr++ = 'x';
85     *ptr++ = itox_chars[(value >> 12) & 0xf];
86     *ptr++ = itox_chars[(value >>  8) & 0xf];
87     *ptr++ = itox_chars[(value >>  4) & 0xf];
88     *ptr++ = itox_chars[(value >>  0) & 0xf];
89     *ptr = 0;
90     return ZERR_NONE;
91 }
92