Monday, May 24, 2010

How to write a program in C by turning a hexadecimal to a decimal?

im having a hard time with this right now. help





write a c program that utilizes strings and numeric conversions





1. Read strings using a function you write called getStr ().


2. Write a function named hextodecimal () that takes in (only) a string parameter representing a positive hexadecimal number and return its decimal equivalent (integer). This function returns -1 if the string provided connotes to a negative number and/or is comprised of invalid characters.


-note: you may not use any library functions in this function- you must write all code necessary to enact the coversion and return the appropriate result.


-this function must accept both upper and lower case versions of the hex character.


-You may want to write an additional function that vets any string for invalid hex character


3. If original string is invalid, output an unambiguous message to the user and prompt for another input.


4. Output the original String, followed by its decimal equivalent. Clearly label which is which

How to write a program in C by turning a hexadecimal to a decimal?
I don't want to give you the entire solution, but I will give you some pieces to help, and comments where you need to fill in the code. This assignment justifiably restricts you from using library functions, which you would normally use to do much of the work for you. Use of fgets to get the user input is, I assume, allowed.





Note that I put the validity check in getStr, but part 2 of the problem statement wants you to check validity in hextodecimal, which is probably a better place to put it.





#include %26lt;stdio.h%26gt;


#include %26lt;string.h%26gt;





typedef enum { false = 0, true } Bool;





double Pow(const double x, const unsigned y);


int AtoI(const char c);


Bool isValidHexChar(const char c);


Bool getStr(char *s);


int hexToDec(char *s);





#define MAX_STR 256





int main(int argc, char *argv[]) {


char s[MAX_STR];





memset(s,0,sizeof(s));


if (getStr(s) == false) {


puts("\ninvalid entry");


} else {


printf("\n%s = %d\n",s,hexToDec(s));


}


return 0;


}





Bool getStr(char *s) {


char *p = s;


Bool isValidFlg = true;





/* prompt for hex value and read it using fgets */


/* remove '\n' from end of entered string */


/* loop for each character in the string, */


/* calling isValidHexChar( ) to check validity */





return isValidFlg;


}





int hexToDec(char *s) {





/* loop from the last char in s to the first, */


/* calling AtoI and Pow as you go to convert */


/* from hex to decimal */





return result;


}





double Pow(const double x, const unsigned y) {


double result = 1;


int i;





if (y %26gt; 0) {


for (i = 0; i %26lt; y; i++) {


result *= x;


}


}


return result;


}





int AtoI(const char c) {


int i = -1;


if (isValidHexChar(c) == true) {


if ((c %26gt;= '0') %26amp;%26amp; (c %26lt;= '9')) i = (int)c - 0x30;


else if ((c %26gt;= 'a') %26amp;%26amp; (c %26lt;= 'f')) i = (int)c - 0x57;


else i = (int)c - 0x37;


}


return i;


}





Bool isValidHexChar(const char c) {


Bool validFlg = false;


if (((c %26gt;= '0') %26amp;%26amp; (c %26lt;= '9')) ||


((c %26gt;= 'a') %26amp;%26amp; (c %26lt;= 'f')) ||


((c %26gt;= 'A') %26amp;%26amp; (c %26lt;= 'F'))) {


validFlg = true;


}


return validFlg;


}
Reply:try to avoid hexadecimal value....


No comments:

Post a Comment