[Coco] Converting Dec to Binary
Barry Nelson
barry.nelson at amobiledevice.com
Thu Mar 22 01:09:42 EDT 2018
A number base converter program…
============================
/* Program to convert Base Numbers */
/* J.D.Bowman 08/23/84 */
/* Translated to UNIX by.......... */
/* The modem brothers */
/* Jorge Lezcano and Barry Nelson */
#ifdef SYS5
#define index strchr
#endif
#include <stdio.h>
char bufin [17] , bufout [17];
char clear[256];
int b;
unsigned i;
char *type;
void dechex();
void hexdec();
void decbin();
void bindec();
void hexbin();
void binhex();
void wait();
void intin();
void itoh();
void display();
void hexin();
unsigned htoi();
void itoa();
void itob();
void binin();
unsigned btoi();
extern int tgetent();
extern char *strcpy();
extern size_t strlen();
extern int atoi();
extern void exit();
extern char *index();
extern char *strcat();
extern int fpurge();
int main ()
{
char val[3];
int vali ;
char *getenv();
char *tgetstr();
char *term;
char *point;
char buff[1024];
int l;
term=getenv("TERM");
if (term==NULL)
term="dumb";
tgetent(buff,term);
point=buff;
strcpy(clear,tgetstr("cl",&point));
if (clear[0]!='#')
{
l=0;
while (l<strlen(clear))
clear[l++] &= 127;
}
else
strcpy(clear,"\f");
b = 0 ;
while (b != 2)
{
puts(clear);
printf(" Base Number Conversion by\n J.D.Bowman");
printf("\nWritten in 'C' language 08/84");
printf("\nTranslated to Unix by the modem brothers 1989.");
printf("\n\n MENU\n");
printf("\n 1> Decimal to Hex");
printf("\n 2> Hex to Decimal");
printf("\n 3> Decimal to Binary");
printf("\n 4> Binary to Decimal");
printf("\n 5> Hex to Binary");
printf("\n 6> Binary to Hex");
printf("\n 0> End Session\n");
printf("\n Enter Your Choice...");
fgets(val,2,stdin);
point=index(val,'\n');
if (point!=NULL) {
point='\0';
}
vali = atoi(val);
if (vali == 1)
{dechex();}
if (vali == 2)
{hexdec();}
if (vali == 3)
{decbin();}
if (vali == 4)
{bindec();}
if (vali == 5)
{hexbin();}
if (vali == 6)
{binhex();}
if (vali == 0)
exit(0);
if (vali > 6)
printf("\nMust enter numeric (0 to 6).");
if (vali > 6) { wait(); }
}
}
void dechex()
{
type="Decimal to Hex";
intin();
i = atoi(bufin);
itoh(i,bufout);
display();
wait();
}
void hexdec()
{
type="Hex to Decimal";
hexin();
i = htoi(bufin);
itoa(i,bufout);
display();
wait();
}
void decbin()
{
type="Decimal to Binary";
intin();
i = atoi(bufin);
itob(i,bufout);
display();
wait();
}
void bindec()
{
type="Binary to Decimal";
binin();
i=btoi(bufin);
itoa(i,bufout);
display();
wait();
}
void hexbin()
{
type="Hex to Binary";
hexin();
i=htoi(bufin);
itob(i,bufout);
display();
wait();
}
void binhex()
{
type="Binary to Hex";
binin();
i=btoi(bufin);
itoh(i,bufout);
display();
wait();
}
void binin()
{
char *point;
fpurge(stdin);
printf("\nEnter 16 bit Binary number:\n");
fgets(bufin,16,stdin);
point=index(bufin,'\n');
if (point!=NULL) {
point='\0';
}
}
void intin()
{
char *point;
fpurge(stdin);
printf("\nEnter Decimal Integer:\n");
fgets(bufin,16,stdin);
point=index(bufin,'\n');
if (point!=NULL) {
point='\0';
}
}
void hexin()
{
char *point;
fpurge(stdin);
printf("\nEnter Hex Integer:\n");
fgets(bufin,16,stdin);
point=index(bufin,'\n');
if (point!=NULL) {
point='\0';
}
}
void display()
{
printf("\n%s%s",clear,type);
printf("\nConversion of: ");
puts(bufin);
printf("\nEquals : ");
puts(bufout);
}
void clears()
{ puts(clear); }
void wait()
{
fpurge(stdin);
printf("\nPress <Enter> to continue... ");
getchar();
}
void itoh(in,buff)
unsigned in;
char *buff;
{
sprintf(buff,"%x",in);
}
unsigned htoi(buff)
char *buff;
{
unsigned o;
sscanf(buff,"%x",&o);
return(o);
}
void itob(in,buff)
unsigned in;
char *buff;
{
char *bin[16];
char temp[5];
char *digits;
int d;
int n;
bin[0]="0000";
bin[1]="0001";
bin[2]="0010";
bin[3]="0011";
bin[4]="0100";
bin[5]="0101";
bin[6]="0110";
bin[7]="0111";
bin[8]="1000";
bin[9]="1001";
bin[10]="1010";
bin[11]="1011";
bin[12]="1100";
bin[13]="1101";
bin[14]="1110";
bin[15]="1111";
digits="0123456789abcdef";
strcpy(buff,"");
sprintf(temp,"%x",in);
n=0;
while (n<strlen(temp))
{
d=index(digits,temp[n++])-index(digits,'0');
strcat(buff,bin[d]);
}
}
unsigned btoi(buff)
char *buff;
{
unsigned o;
int n;
long p;
p=1;
o=0;
n=strlen(buff)-1;
while (n>=0)
{
if (buff[n--]=='1')
o+=p;
p+=p;
}
return(o);
}
void itoa(in,buff)
unsigned in;
char *buff;
{
sprintf(buff,"%d",in);
}
More information about the Coco
mailing list