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  *	Copyright (c) 1987 by the Massachusetts Institute of Technology.
7  *	For copying and distribution information, see the file
8  *	"mit-copyright.h".
9  */
10 
11 #include "internal.h"
12 
13 static char *itox_chars = "0123456789ABCDEF";
14 
ZMakeAscii(ptr,len,field,num)15 Code_t ZMakeAscii(ptr, len, field, num)
16     register char *ptr;
17     int len;
18     unsigned char *field;
19     int num;
20 {
21     int i;
22 
23     for (i=0;i<num;i++) {
24 	/* we need to add "0x" if we are between 4 byte pieces */
25 	if ((i & 3) == 0) {
26 	    if (len < (i?4:3))
27 		return ZERR_FIELDLEN;
28 	    /* except at the beginning, put a space in before the "0x" */
29 	    if (i) {
30 		*ptr++ = ' ';
31 		len--;
32 	    }
33 	    *ptr++ = '0';
34 	    *ptr++ = 'x';
35 	    len -= 2;
36 	}
37 	if (len < 3)
38 	    return ZERR_FIELDLEN;
39 	*ptr++ = itox_chars[(int) (field[i] >> 4)];
40 	*ptr++ = itox_chars[(int) (field[i] & 0xf)];
41 	len -= 2;
42     }
43 
44     *ptr = '\0';
45     return ZERR_NONE;
46 }
47 
ZMakeAscii32(ptr,len,value)48 Code_t ZMakeAscii32(ptr, len, value)
49     register char *ptr;
50     int len;
51     unsigned long value;
52 {
53     if (len < 11)
54 	return ZERR_FIELDLEN;
55     *ptr++ = '0';
56     *ptr++ = 'x';
57     *ptr++ = itox_chars[(value >> 28) & 0xf];
58     *ptr++ = itox_chars[(value >> 24) & 0xf];
59     *ptr++ = itox_chars[(value >> 20) & 0xf];
60     *ptr++ = itox_chars[(value >> 16) & 0xf];
61     *ptr++ = itox_chars[(value >> 12) & 0xf];
62     *ptr++ = itox_chars[(value >>  8) & 0xf];
63     *ptr++ = itox_chars[(value >>  4) & 0xf];
64     *ptr++ = itox_chars[(value >>  0) & 0xf];
65     *ptr = 0;
66     return ZERR_NONE;
67 }
68 
ZMakeAscii16(ptr,len,value)69 Code_t ZMakeAscii16(ptr, len, value)
70     register char *ptr;
71     int len;
72     unsigned int value;
73 {
74     if (len < 7)
75 	return ZERR_FIELDLEN;
76     *ptr++ = '0';
77     *ptr++ = 'x';
78     *ptr++ = itox_chars[(value >> 12) & 0xf];
79     *ptr++ = itox_chars[(value >>  8) & 0xf];
80     *ptr++ = itox_chars[(value >>  4) & 0xf];
81     *ptr++ = itox_chars[(value >>  0) & 0xf];
82     *ptr = 0;
83     return ZERR_NONE;
84 }
85 
86