Thread: hex to binary,hex to decimal

  1. #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) {

    }
    }

  2. #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

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Code tags use [ ] on this forum, not < >

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Decimal to Hex Converter
    By rocketman03 in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:50 AM
  2. Replies: 11
    Last Post: 03-24-2006, 11:26 AM
  3. Converting decimal to hex
    By cobrakidd in forum C++ Programming
    Replies: 9
    Last Post: 02-06-2003, 11:37 AM
  4. about decimal to hex progam
    By Abdi in forum C Programming
    Replies: 2
    Last Post: 06-01-2002, 07:08 PM
  5. Decimal vs. Hex
    By -leech- in forum C Programming
    Replies: 6
    Last Post: 02-18-2002, 12:51 PM