# Thread: hex to binary,hex to decimal

1. ## hex to binary,hex to decimal

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':
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;
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;
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;
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;
printf("%s\n", string);
break;
}
return(0);
}

}
void hex2bin(char *string, char *binstring) {

}
void hex2dec(char *string, int *dvalue) {

}
}

2. A bit of indentation would help here...

Your function definition for isHEXADEC() is missing the "char" before the "*".

You've got at least one exta "}" at the end of the file.

You're trying to print strings with "%d", which will not give you the result you seem to expect.

Assuming that you define a binary string as being a string of "0" and "1" characters, with possible (meaningless) spaces before and/or after the string, a decimal string as a string of "0" - "9" digits and an optional sign character ("+" or "-") prefix (or postfix, if you like, again with space padding), and a hex string being the digits "0" - "9" and the letters "A" - "F" and "a" - "f", you can start doing your "isXXX" functions by checking for that.

Personally, I think that your program, so far, needs a lot of work. The first step that I'd take is to sit down with pencil and paper and determine exactly what you're trying to do -- are you converting from one string form to another, regardless of whether the value of that string can be represented in an integer storage type on the machine you're running it on? Are you converting to an internal representation and then displaying it in various forms (bin, dec, hex)? You seem to be undecided from my reading of your code.

3. Code tags use [ ] on this forum, not < >

Popular pages Recent additions