Hexadecimal function problem

This is a discussion on Hexadecimal function problem within the C Programming forums, part of the General Programming Boards category; Hi everyone, I am working on a little function that calculates a decimal value of a hexadecimal input, when I ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    28

    Hexadecimal function problem

    Hi everyone,

    I am working on a little function that calculates a decimal value of
    a hexadecimal input, when I run the program it gives me negative
    numbers and I cann't figure out why it doing so:

    can anyone have look at my code and have look what I am getting wrong ?

    here is the code with test main:
    Code:
    code
    
    void main(void)
    {
           int X;
           int Y;
    
           printf("enter  a hex value :  ");
           scanf("%x", & Y);
          
           X = Convert( Y); 
           printf(" corresponding decimal value is %d\n",  X);
    }    
    
    
    int Convert(char val )
    {
          
       if ((val >= 0 ) && (val <= 9))
       {
           val = (int)val - 0; 
           return val ;
       }
       else if(val=='A'||val=='B'|| val=='C'||val=='D'||val=='E'||val=='F' )
       {   
           val = (int)val - (int)('A') + 10;
           return val; 
       }
             
    }
    Thanks in advance

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    You've over complicated things....... Have a look at this version:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	long int X;
    
    	printf("Enter a hex value : ");
    	if (scanf("%x", &X) == 1)
    		printf ("You entered %d\n", X);
    	return 0;
    }
    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
    Apr 2002
    Posts
    28

    I need Hex function to work

    Hi Hammer,

    I want definitely to make hex function work how can I achieve that the way you showed me is the way I could output(formatting) is that correct ? ....why it gives me negative numbers ?

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    85

    Red face

    + You passed to the funtion call with mismatch
    data type.
    it is an integer Y, not a character Y.
    Here is your call:
    X = Convert(Y);
    while you define the Convert funtion as:
    int Convert(char val )

    I think you should redefine as:

    int Convert(int val)
    {
    }

    + Also, you might think of defining the formal parameter and actual parameter as UNSIGNED INT.

    I think it should work!

    DV007

    /*=======================================*
    * Say What You Mean. Mean What You Say. *
    *=======================================*/

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: I need Hex function to work

    Originally posted by Abdi
    Hi Hammer,

    I want definitely to make hex function work how can I achieve that the way you showed me is the way I could output(formatting) is that correct ? ....why it gives me negative numbers ?
    If you don't want to do it the easy way (like in my example), you can do it the hard way of reading input as a string, the checking each character in the string to ensure it'sa valid hex characters, then perform the appropriate maths to get the decimal version.

    All in all, I think you should stick with scanf(), or maybe sscanf is you want to read input as a string. This then does all the hard work for you.
    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. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  2. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 11:03 AM
  4. Problem with function pointers
    By vNvNation in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2004, 07:49 AM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM

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