Thread: Conversion of hex to decimal

  1. #1
    Registered User
    Join Date
    Apr 2015
    Posts
    3

    Question Conversion of hex to decimal

    Hi
    I have been trying to convert hexadecimal to decimal value just i need to get 5 input hex values and store it in an array and convert them to decimal values, as i compiled i am getting error as "Time limit exceeded".And i tried this on turbo c ,am getting error as "character constant must be one or two characters long".

    Code:
    #include <stdio.h>
    #include<math.h>
    #include<string.h>
    
    int main(void) {
        int i,j,num,len,counter=0;
        long int sum=0;  //value can cross range of int
         char hex[100]={'1ED7','1ED8','1ED6','1ED5','1ED6'};
    
            //conversion to dec
        len=strlen(hex);
        printf("%d",len);
            for(i=0;i<5;i++)
        {
    
        for(i=len-1;i>=0;i--)
        {
           if(hex[i]>='0'&&hex[i]<='9')
              num=hex[i]-'0';     //the character is number in between 0 to 9
           else
           {
              if(hex[i]>='A'&&hex[i]<='F')
              num=hex[i]-55;      //the character is either a,b,c,d,e or f 
              else
              {
                  printf("The Entered number is not hexadecimal number");
                 // getch();// If the character is beyond 'f'
                  return 1;
              }
           }
           sum=sum+pow(16,counter)*num;  //according to formula given by the link
           counter++;
        } 
        }
        printf("Hexadecimal conversion of %s to decimal is %ld",hex,sum);
        return 0;
    }
            
            //conversion to weight
    //    }
    //    return 0;
    //}*/
    pls help?!

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    On line 8, this isn't how you express hexadecimal values, nor how you express a string. Either convert that array to int, remove the quotes and prefix the numbers with 0x or use double quotes and merge them all together in the array.

    EDIT: Why am I saying to convert the array to int? Because 0x1ED5 is way larger than 255(0xFF), which is the maximum value a char can hold( most of the time ).
    Last edited by GReaper; 04-16-2015 at 02:36 AM.
    Devoted my life to programming...

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by bharockz
    i need to get 5 input hex values and store it in an array and convert them to decimal values
    I think that you need to be clear: are you trying to convert a numeric string in hexadecimal representation to an integer value? For example, given the input "1ED7" (stored in an array of char), you want to output 7895 (as a long int expressed in decimal representation).

    If so, then GReaper's suggestion in post #2 does not apply. Rather, you should first declare a single array of char to hold a single hexadecimal string, e.g.,
    Code:
    char hex[100] = "1ED7";
    Then work on converting this one hexadecimal string to long int. In fact, I would suggest writing a function:
    Code:
    long int convertHexToLong(const char *hex);
    along the same lines as atoi (though unlike atoi you may wish to augment this with better error handling later).
    Last edited by laserlight; 04-16-2015 at 02:41 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Decimal to Hex Conversion
    By millsy2000 in forum C Programming
    Replies: 7
    Last Post: 08-10-2010, 01:04 PM
  2. Decimal to hexadecimal conversion
    By Nathalie in forum C Programming
    Replies: 9
    Last Post: 12-11-2007, 03:29 PM
  3. Conversion of hexidecimal to decimal
    By twocool82 in forum C Programming
    Replies: 6
    Last Post: 07-09-2007, 03:33 AM
  4. decimal to binary conversion
    By noob2c in forum C Programming
    Replies: 4
    Last Post: 05-29-2003, 08:07 PM
  5. Binary to decimal conversion help!
    By matrism in forum C Programming
    Replies: 4
    Last Post: 03-25-2002, 12:22 PM

Tags for this Thread