[fa.info-mac] FROMHEX.C and TOHEX.C

info-mac@uw-beaver (info-mac) (11/08/84)

From: Ed Pattermann <PATTERMANN@SUMEX-AIM.ARPA>
FROMHEX.C

#include "quickdraw.h"
#include "osintf.h"
#include "toolintf.h"

#define bufsize 512
#define InName "C"
#define OutName "C prog"

int rout, rin, io, i;
int l, val, bytes, sum, getsum;
char bin[bufsize], bout[bufsize];
int count, szin, bini, bouti;
FInfo fInfo;

putflush()
   {
   if (bouti)
      {
      count = bouti;
      io = FSWrite(rout, &count, bout); 
      bouti = 0;
      }
   }

putchar(ch)
   int ch;
   {
   bout[bouti++] = (char) ch;
   if (bouti == bufsize)
      putflush();
   }

int getchar()
   {
   if (bini == bufsize)
      {
      bini = 0;
      count = bufsize;
      io = FSRead(rin, &count, bin);
      }
   return ((int) bin[bini++]);
   }

badnews()
   {
   int j;

   io = FSClose(rin);
   io = FSClose(rout);
   SysBeep(1);
   for (j = 0; j <= 20000; j++) 
      l = 0;
   SysBeep(1);
   ExitToShell();
   }

main()
   {
   bouti = 0;
   bini = bufsize;

   io = FSDelete(OutName, 0);
   io = Create(OutName, 0, "CCOM", "APPL");
   io = GetFInfo(OutName, 0, &fInfo);
   fInfo.fdFlags |= fHasBundle << 8;
   io = SetFInfo(OutName, 0, &fInfo);
   io = OpenRF(OutName, 0, &rout);
   if (io)
      badnews();

   io = FSOpen(InName, 0, &rin);
   if (io)
      badnews();
   io = GetEOF(rin, &szin);
   count = szin >> 1;
   io = Allocate(rout, &count);
   if (io)
      badnews();
   val = 0;
   bytes = 0;
   sum = 0;
   getsum = 0;

   while (szin && !getsum)
      {
      l = getchar() & 127;
      szin--;
      if ((l >= 64) && (l < 80))
         {
	 bytes++;
         val = (val << 4) | (l - 64);
	 if (!(bytes & 1))
	    {
	    putchar(val);
	    sum += val;
	    val = 0;
 	    }
	 }
      if (l == 124)
         getsum = 1;
      }
   putflush();
   io = FSClose(rout);
   if (!getsum)
      badnews();

   sum += bytes >> 1;
   val = 0;
   for (i = 1; i <= 8; i++)
      val = (val << 4) | (getchar() & 15);
   io = FSClose(rin);
   if (val != sum)
      badnews();
   ExitToShell();
   }

TOHEX.C

#include <stdio.h>

char hex[] = "@ABCDEFGHIJKLMNO";
int bytes,sum;
unsigned long htonl();

main(argc,argv)
	char **argv;
{
	register i,len;
	register char *cp;

	len = 0;
	while ((i = getchar()) != EOF) {
		bytes++;
		sum += i;
		putchar(hex[i>>4]);
		putchar(hex[i&0xF]);
		if (++len > 32) {
			putchar('\n');
			len = 0;
		}
	}
	fprintf(stderr, "bytes %d, sum %d\n", bytes, sum);
	putchar('|');
	sum += bytes;
	sum = htonl(sum);
	cp = (char *)&sum;
	for (len = 0 ; len < 4 ; len++) {
		i = (*cp++ & 0xff);
		putchar(hex[i>>4]);
		putchar(hex[i&0xF]);
	}
	putchar('\n');
	exit(0);
}

#define nohtonl
#ifdef nohtonl	/* if not in library */
/*
 * "Host" to "net" byte order swappers.
 */
unsigned short htons(a)
	unsigned short a;
{
	unsigned short result;
	register char *sp = (char *)&a;
	register char *dp = (char *)&result;

	dp[1] = *sp++;
	dp[0] = *sp;
	return (result);
}


unsigned long htonl(a)
	unsigned long a;
{
	unsigned long result;
	register char *sp = (char *)&a;
	register char *dp = (char *)&result;

	dp[3] = *sp++;
	dp[2] = *sp++;
	dp[1] = *sp++;
	dp[0] = *sp;
	return (result);
}
#endif
-------