Category : C Source Code
Archive   : QC_K_R.ZIP
Filename : EX3_04B.C

 
Output of file : EX3_04B.C contained in archive : QC_K_R.ZIP
/* Exercise 3-4, page 60 */

/* precision limited to 16 bits unsigned (0-65535) */

#include
#define MAX 9

main() /* driver to test itoa(n, s) */
{
char line[MAX], string[MAX];
int c;
while ((c = getline(line, MAX)) != 0) {
itoh(htoi(line), string);
printf("%s\n", string);
}
}

itoh(n, s) /* convert n to characters in s */
char s[];
unsigned n;
{
int i, x;
i = 0;
do { /* generate digits in reverse order */
s[i++] = ((x = (n & 0xf)) <= 9) ? x + '0': x - 10 + 'a';
} while ((n >>= 4) != 0);
s[i] = '\0';
reverse(s);
}

reverse(s) /* reverse string s in place */
char s[];
{
int c, i, j;
for (i = 0, j = strlen(s)-1; i < j; i++, j--) {
c = s[i];
s[i] = s[j];
s[j] = c;
}
}

strlen(s) /* determine string length */
char s[];
{
int i;
for (i = 0; s[i] != 0; i++)
;
return(i);
}

htoi(s) /* convert hex(s) to integer */
char s[];
{
int c, i;

unsigned v = 0;
for (i = 0; (c = hex(s[i])) < 16; i++)
v = (v * 16) + c;
return(v);
}

hex(p) /* convert ASCII hexidecimal digit to binary */
char p;
{
if (p >= '0' && p <= '9')
return(p - '0');
else
if ((p = (p & 0xdf)) >= 'A' && p <= 'F')
return(p - '7');
else
return(16);
}

getline(s, lim) /* get line into s, return length */
char s[];
int lim;
{
int c, i;
for (i=0; i s[i] = c;
if (c == '\n')
s[i++] = c;
s[i] = '\0';
return(i);
}


  3 Responses to “Category : C Source Code
Archive   : QC_K_R.ZIP
Filename : EX3_04B.C

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/