Thread: convers. problem.

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    53

    convers. problem.

    Okay, I have actually two problems but after I get the first, I should be able to do the second. First, I have to write code to convert a hexadecimal to a decimal and its binary as well. The following code is what I have to fthe decimal conversion. Thanks for all of your help ahead of time, you guys are the smartest people on the planet. You can avoid main... It is just there for reference, and I know it works.

    Code:
    /*    HEADER  */
    #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 *);
    
    int processBINARY(char *, enum mode_t);
    int processDECIMAL(char *, enum mode_t);
    int processHEXADEC(char *, enum mode_t);
    
    
    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);
        }
    }
    
    
    /*********************************************/
    
    void hex2dec(char *string, int *dvalue)  {
    int slength,i;
    int num[20],exp[20];
    slength=strlen(string);
    
    for (i=0; i<slength; i++)  {
      num[i]=string[i]-48;
    }
    
    for (i=slength; i>=0; i--)  {
      if (i==slength)
        exp[i]=num[i];
      else
        exp[i]=16*num[1];
    }
    for (i=0; i<slength; i++)  
      dvalue+=num[i];
    
    }
    Thanks for any assistance you can give.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    I take it you can't use strtol() ?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    53

    Re:

    I guess, how would i use it ?

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    http://www.rt.com/man/strtol.3.html

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
      char a[] = "abcd";
      long int l;
      
      l = strtol(a, NULL, 16);
      
      printf ("l is %ld\n", l);
      
      return(0);
    }
    [edit]
    If this is homework, I expect your supposed to do the hard work with your own code though
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    53
    now back to the first question, how do i convert a hexadecimal into a decimal? I have completed the other code and only need help with the hex conversions.

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by justin69enoch
    now back to the first question, how do i convert a hexadecimal into a decimal?
    Isn't that what I just showed you? Or are you meaning something else...
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User
    Join Date
    Sep 2002
    Posts
    53
    I appologize, I thought that that was just a sample. It worked great with my code. There isn't a simple function that does hex to binary is there? That's all I like and then I can put it with the other 12 functions and write a print function for everything. Thanks for all of your help... also, if anyone has the code for a hex to binary conversion, I'd gladly appreciate it. When I finish, I'll post it all for everyone. Thanks again.

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    http://www.cprogramming.com/cboard/s...t=print+binary

    there's probably some more posts on here too, try a search or two.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM