Thread: HexToDec/DecToHex, feedback if any, Thanks.

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    61

    HexToDec/DecToHex, feedback if any, Thanks.

    Greetings EveryOne

    I want to present you my Hexadecimal/Decimal converter for feedback.

    HexToDec.c:
    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <stdlib.h>
    #include <math.h>
    
    int main()
    {
    	int	 StrMaxSize = 20;
    	char *HexString = malloc(StrMaxSize);
    	int  *HexNumbAt = malloc(StrMaxSize);
    	char ValidHexNumber[16] = {'0', '1', '2', '3', '4', 
    							   '5', '6', '7', '8', '9',
    				   		   	   'a', 'b', 'c', 'd', 'e', 
    						       'f'};
    
    	int  CurrentStrSize = StrMaxSize-2;
    	
    	int DecNumb = 0;
    	
    	int NotValidHexValue = 0;
    	
    	unsigned int Cnt = 0;
    	
    	int i = 0;
    	int k = 0;
    	
    	printf("Please Enter Your Hexadecimal Number: ");
    	if(HexString != NULL && HexNumbAt != NULL)
    	{
    		int Char = EOF;
    		
    		while((Char = getchar()) != '\n' && Char != EOF)
    		{
    			// Input Hexadecimal Number;
    			HexString[Cnt] = (char) Char;
    			Cnt++;
    			if(Cnt == CurrentStrSize)
    			{
    				CurrentStrSize = (Cnt+StrMaxSize)-2;
    				
    				HexString = realloc(HexString, CurrentStrSize);
    				HexNumbAt = realloc(HexNumbAt, CurrentStrSize);
    			}
    		}
    		HexString[Cnt] = '\0';
    	}
    
    	// Display the entered Hexadecimal Number;
    	printf("HexNumber = %s\n", HexString);
    
    		
    	// Check for the validity of the entred Hexadecimal Number;
    	for(i = 0; i < Cnt; i++)
    	{
    		char LowerCaseChar = tolower(HexString[i]);
    		
    		int j = 0;		
    		for(j = 0; j < 16; j++)
    		{
    			if(LowerCaseChar == ValidHexNumber[j])
    			{
    				break;
    			}
    			
    			if(j == 15)
    			{
    				NotValidHexValue = 1;
    			}
    		}
    		
    		if(NotValidHexValue)
    		{
    			printf("Error: Unrecognized Value\n");
    			NotValidHexValue = 0;
    			return -1;  // exit program with (-1);
    		}
    	}
    	
    	
    	// Convert Hex to Dec;
    	for(i=Cnt-1; i >= 0; i--)
    	{
    		int x = 0;
    		
    		if(isdigit(HexString[i]))
    		{
    			char Ch[2] = {0};
    			Ch[0] = HexString[i];
    			Ch[1] = '\0';
    			x = atoi(Ch);
    			
    			HexNumbAt[i] = (x*pow(16,k));
    		}
    		
    		if(isalpha(HexString[i]))
    		{
    			switch(tolower(HexString[i]))
    			{
    				case 'a':
    					 x = 10;
    					 HexNumbAt[i] = (x*pow(16,k));
    					 break;
    					case 'b':
    						 x = 11;
    						 HexNumbAt[i] = (x*pow(16,k));
    						 break;
    					case 'c':
    						 x = 12;
    						 HexNumbAt[i] = (x*pow(16,k));
    						 break;
    					case 'd':
    						 x = 13;
    						 HexNumbAt[i] = (x*pow(16,k));
    						 break;
    					case 'e':
    						 x = 14;
    						 HexNumbAt[i] = (x*pow(16,k));
    						 break;
    					case 'f':
    						 x = 15;
    						 HexNumbAt[i] = (x*pow(16,k));
    						 break;
    				}
    		}
    		k += 1;
    		DecNumb += HexNumbAt[i];
    	}
    
    	// Display the Decimal converted number;
    	printf("DecNumb = %d\n", DecNumb);
    	
    	return 0;
    }

    DecToHex.c:
    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <stdlib.h>
    #include <math.h>
    
    int main()
    {
    	int    DecNumb     = 0;
    	char   HexNumb[20] = {0};
    	double Quotient = 1;
    	int	   Reminder = 0;
    	
    	printf("Please Enter A Decilam: ");
    	scanf("%d", &DecNumb);
    	
    	int i = 0;
    	Quotient = DecNumb;
    	while(Quotient != 0)
    	{
    		Reminder = ((int) Quotient)%16;
    		Quotient = Quotient/16;
    		
    		Quotient = (int) Quotient;
    		
    		if(Reminder >= 0 && Reminder <= 9)
    		{
    			sprintf(&HexNumb[i], "%d", Reminder);
    		}
    		else
    		if(Reminder >= 10 && Reminder <= 15)
    		{
    			switch(Reminder)
    			{
    				case 10: HexNumb[i] = 'A'; break;
    				case 11: HexNumb[i] = 'B'; break;
    				case 12: HexNumb[i] = 'C'; break;
    				case 13: HexNumb[i] = 'D'; break;
    				case 14: HexNumb[i] = 'E'; break;
    				case 15: HexNumb[i] = 'F'; break;
    				default: printf("Error: Unrecognized Hex Digit\n");
    						 printf("Reminder = %d", Reminder);
    						 return -1;
    						 
    			}
    		}
    		
    		i++;
    	}
    	HexNumb[i] = '\0';
    	
    	printf("HexNumb = ");
    	while(i >= 0)
    	{
    		printf("%c", HexNumb[i]);
    		i--;
    	}
    	printf("\n");
    	return 0;
    }
    Thank You for your feedback if there is any
    Knowledge Is Power!

    Don't give me a fish but teach me how to catch one

    I am not asking you to code it for me, what i need is guidance so i can learn

  2. #2
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    Sorry, I couldn't help myself...

    hextodec.c:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	int val;	
    	printf("Enter a hexadecimal number: ");	
    	scanf("%x", &val);
    	printf("%d\n", val);
    	
    	return 0;
    }
    dectohex.c:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	int val;	
    	printf("Enter a decimal number: ");	
    	scanf("%d", &val);
    	printf("%x\n", val);
    	
    	return 0;
    }
    As for comments, one thing I noticed is that you didn't use character - '0' to get digits. The possibility of non-ASCII character sets aside, (I haven't seen any on a modern system, have you?) it's a neat trick to know for a quick digit-to-character or character-to-digit conversion.
    Last edited by bernt; 01-19-2012 at 07:32 PM.
    Consider this post signed

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    61
    As for comments, one thing I noticed is that you didn't use character - '0' to get digits. The possibility of non-ASCII character sets aside, (I haven't seen any on a modern system, have you?) it's a neat trick to know for a quick digit-to-character or character-to-digit conversion.
    I don't understand what you are talking about can you explain further please.

    Thanks for your reply.
    Knowledge Is Power!

    Don't give me a fish but teach me how to catch one

    I am not asking you to code it for me, what i need is guidance so i can learn

  4. #4
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    Characters are laid out contiguously and in order '0' through '9', so, eg. (int)('0' - '0') = 0, and (int)('5' - '0') = 5.
    Given a character 'n' representing some digit 0-9, (int)('n' - '0') = n. It's just something I prefer over functions life sscanf() or atoi() in this particular case.
    Consider this post signed

  5. #5
    Registered User
    Join Date
    Dec 2010
    Posts
    61
    Quote Originally Posted by bernt View Post
    Characters are laid out contiguously and in order '0' through '9', so, eg. (int)('0' - '0') = 0, and (int)('5' - '0') = 5.
    Given a character 'n' representing some digit 0-9, (int)('n' - '0') = n. It's just something I prefer over functions life sscanf() or atoi() in this particular case.
    I understand now, Thank you very much for the tip

    P.S: i would been got it the first time if it was presented like this:
    one thing I noticed is that you didn't use (character - '0') to get digits.

    That's why i got confused, Thank you again.
    Last edited by Laythe; 01-19-2012 at 09:09 PM.
    Knowledge Is Power!

    Don't give me a fish but teach me how to catch one

    I am not asking you to code it for me, what i need is guidance so i can learn

  6. #6
    Lurker
    Join Date
    Dec 2004
    Posts
    296
    Quote Originally Posted by bernt View Post
    Characters are laid out contiguously and in order '0' through '9', so, eg. (int)('0' - '0') = 0, and (int)('5' - '0') = 5.Given a character 'n' representing some digit 0-9, (int)('n' - '0') = n. It's just something I prefer over functions life sscanf() or atoi() in this particular case.
    I don't like this way of doing things as they are not portable. Sure, it will work on most systems used today, but what about 2 years from now? I mean, there are other encoding systems then ASCII... If there are a standard library function to convert things, then use it. I don't know how many times I have fixed bugs in peoples code that stem from people making assumptions. Stuff like assuming an int is a certain lenght, that you can represent the date with 2 digits and that you can do - '0' to get a digit, they are _all_ bad. Sorry if my tone sounds kind of harsh, it is not meant to, but I think it is better to learn that lesson sooner rather than later and this way rather than the hard way.

  7. #7
    Registered User
    Join Date
    Dec 2010
    Posts
    61
    I don't like this way of doing things as they are not portable. Sure, it will work on most systems used today, but what about 2 years from now? I mean, there are other encoding systems then ASCII... If there are a standard library function to convert things, then use it. I don't know how many times I have fixed bugs in peoples code that stem from people making assumptions. Stuff like assuming an int is a certain length, that you can represent the date with 2 digits and that you can do - '0' to get a digit, they are _all_ bad. Sorry if my tone sounds kind of harsh, it is not meant to, but I think it is better to learn that lesson sooner rather than later and this way rather than the hard way.
    I agree with you on this Jimmy, and i had read something similar to this matter, like that an int may be presented in the memory with 2-bits or 4-bits, it can all be dependent from computer to another, thank you for the note.
    Knowledge Is Power!

    Don't give me a fish but teach me how to catch one

    I am not asking you to code it for me, what i need is guidance so i can learn

  8. #8
    Registered User
    Join Date
    Jun 2010
    Location
    Michigan, USA
    Posts
    143
    Quote Originally Posted by Jimmy View Post
    I don't like this way of doing things as they are not portable.
    Agreed that either be portable or be clear in comments or conditional compilation that it is not.
    Quote Originally Posted by Jimmy View Post
    I mean, there are other encoding systems then ASCII...
    The classic alternative is EBCDIC still used on some systems.
    Quote Originally Posted by Jimmy View Post
    If there are a standard library function to convert things, then use it.
    Agreed.
    Quote Originally Posted by Jimmy View Post
    Stuff like assuming an int is a certain lenght, that you can represent the date with 2 digits and
    Or that CHAR_BIT is 8 (number of bits in a char type) which is found in <limits.h>.
    Quote Originally Posted by Jimmy View Post
    that you can do - '0' to get a digit,
    but the decimal digits from '0' to '9' are sequential is required by the programming language standard (for example in C99 section 5.2.1p3). This is true for ASCII and all of the sets derived from it and EBCDIC.

  9. #9
    Lurker
    Join Date
    Dec 2004
    Posts
    296
    Quote Originally Posted by pheininger View Post
    Agreed that either be portable or be clear in comments or conditional compilation that it is not. The classic alternative is EBCDIC still used on some systems. Agreed. Or that CHAR_BIT is 8 (number of bits in a char type) which is found in .but the decimal digits from '0' to '9' are sequential is required by the programming language standard (for example in C99 section 5.2.1p3). This is true for ASCII and all of the sets derived from it and EBCDIC.
    You are kind of missing my point. Will you always be sure that what is true today is true tomorrow? 1 year from now? 10 years from now? I have worked on software in production that is 15 years old or more. What was true 15 years ago might not be true today. If you have the possibility to use standardized library functions that abstract away knowledge, instead of cute tricks, then go for standardized every day of the week. Cute tricks are what are going to bite when you least expect it. What I tried to argue against was somebody telling the OP to use a "trick" instead of a library function. Ofc you could always argue that atoi is not a good function to use anyway, but that is another discussion.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Looking For Feedback
    By Matty_Alan in forum C++ Programming
    Replies: 2
    Last Post: 05-27-2010, 04:33 PM
  2. K&R2, 1-24. Feedback
    By Tool in forum C Programming
    Replies: 0
    Last Post: 11-14-2009, 10:49 AM
  3. Feedback?
    By CornedBee in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 01-02-2004, 11:01 PM
  4. feedback, please...
    By major_small in forum C++ Programming
    Replies: 5
    Last Post: 08-24-2003, 08:17 PM
  5. Looking for some feedback...
    By dead_cell in forum C++ Programming
    Replies: 7
    Last Post: 08-11-2002, 09:08 AM