C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 01-24-2006, 11:53 AM   #1
Registered User
 
Join Date: Sep 2005
Posts: 3
Exclamation 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':
*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) {

}
}
groovy is offline   Reply With Quote
Old 01-24-2006, 01:28 PM   #2
Sr. Software Engineer
 
filker0's Avatar
 
Join Date: Sep 2005
Location: West Virginia
Posts: 235
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.
__________________
Insert obnoxious but pithy remark here
filker0 is offline   Reply With Quote
Old 01-25-2006, 02:14 AM   #3
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,693
Code tags use [ ] on this forum, not < >
Salem is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Decimal to Hex Converter rocketman03 C Programming 1 10-25-2008 03:50 AM
No atoh() function in C ( Ascii To Hex )? - Well, Let's Create One dedham_ma_man C Programming 11 03-24-2006 11:26 AM
Converting decimal to hex cobrakidd C++ Programming 9 02-06-2003 11:37 AM
about decimal to hex progam Abdi C Programming 2 06-01-2002 07:08 PM
Decimal vs. Hex -leech- C Programming 6 02-18-2002 12:51 PM


All times are GMT -6. The time now is 06:40 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22