I just need any advice or guidance anyone has to writing the function to do this. Thanks!
Here is what I have so far:
<code>
#define ARRAYSZ 128
enum mode_t { BINARY, DECIMAL, HEXADEC };
enum boolean_t { FALSE, TRUE };
int setmode(char, enum mode_t *);
enum boolean_t isBINARY(char *);
enum boolean_t isDECIMAL(char *);
enum boolean_t isHEXADEC(char *);
void bin2dec (char *, int *);
void dec2bin (int, char *);
void bin2hex (char *, char *);
void hex2bin (char *, char *);
void hex2dec (char *, int *);
int processBINARY (char *, enum mode_t);
int processDECIMAL (char *, enum mode_t);
int processHEXADEC (char *, enum mode_t);
</code>
<br>
<code>
#include <stdio.h>
#include <string.h>
#include "nc.h"
int setmode (char c, enum mode_t *mode) {
switch (c) {
case 'b':
*mode = BINARY;
break;
case 'd':
*mode = DECIMAL;
break;
case 'h':
*mode = HEXADEC;
break;
default:
return(1);
}
return(0);
}
int main (int argc, char *argv[]) {
char *errmsg = "input error";
char string[ARRAYSZ];
int errstat;
enum mode_t imode = DECIMAL, omode = DECIMAL;
while (scanf("%s", string) != EOF) {
if (strncmp("?", string, 1) == 0) {
printusage(imode, omode);
errstat = 0;
} else if (strncmp("i=", string, 2) == 0) {
errstat = setmode(tolower((int)string[2]), &imode);
} else if (strncmp("o=", string, 2) == 0) {
errstat = setmode(tolower((int)string[2]), &omode);
} else {
switch (imode) {
case BINARY:
errstat = processBINARY(string, omode);
break;
case DECIMAL:
errstat = processDECIMAL(string, omode);
break;
case HEXADEC:
errstat = processHEXADEC(string, omode);
break;
}
}
if (errstat)
printf("%s\n", errmsg);
}
return 0;
}
</code>
<br>
<code>
#include <stdio.h>
#include "nc.h"
enum boolean_t isBINARY (char *input) {
char *cp;
for (cp=input; *cp != '\0' ; cp++){
if (*cp != '0' && *cp != '1')
return FALSE;
}
return TRUE;
}
int processBINARY (char *string, enum mode_t omode) {
int dvalue;
if (isBINARY(string) == FALSE)
return(1);
switch (omode) {
case BINARY:
printf("%s\n", string);
break;
case DECIMAL:
bin2dec(string, &dvalue);
printf("%d\n", dvalue);
break;
case HEXADEC:
bin2hex(string, string);
printf("%s\n", string);
break;
}
return(0);
}
int processDECIMAL (char *string, enum mode_t omode) {
int dvalue;
if (isDECIMAL(string) == FALSE)
return(1);
switch (omode) {
case BINARY:
dec2bin(string, &dvalue);
printf("%s\n", string);
break;
case DECIMAL:
printf("%d\n", string);
break;
case HEXADEC:
dec2hex(string, string);
printf("%s\n", string);
break;
}
return(0);
}
int processHEXADEC (char *string, enum mode_t omode) {
int dvalue;
if (isHEXADEC(string) == FALSE)
return(1);
switch (omode) {
case BINARY:
hex2bin(string, &dvalue);
printf("%s\n", string);
break;
case DECIMAL:
hex2dec(string, &dvalue);
printf("%d\n", dvalue);
break;
case HEXADEC:
printf("%s\n", string);
break;
}
return(0);
}
void isHEXADEC(*string) {
}
void hex2bin(char *string, char *binstring) {
}
void hex2dec(char *string, int *dvalue) {
}
}