Question
Programming Exercise Chapter 10 Number 2 Page 352 With Enhancements Create a function that converts a string representation of a number from one base to
Programming Exercise Chapter 10 Number 2 Page 352 With Enhancements
Create a function that converts a string representation of a number from one base to another. The function must have the following prototype:
void FromBase(char *originalNumber, char *result,
int originalBase, int resultBase);
Where:
originalNumber is the character string that is to be converted.
result is where the resulting string is to be placed
originalBase is the base of originalNumber
resultBase is the base that the result should be converted to
Examples:
FromBase(110, result, 2, 10);
will place 6 in result (the result of converting 110 from base 2 to base 10)
FromBase(31, result, 10, 16);
will place 1F in result (the result of converting 31 from base 10 to base 16
Clarifications on the hints:
Instead of subtracting magic numbers such as 55 to convert from ASCII to the numeric value, use character constants:
if (isdigit(originalNumber[ii]))
characterValue = originalNumber[ii] '0';
else if (isalpha(originalNumber[ii]))
characterValue = originalNumber[ii] 'A';
Other requirements:
If originalNumber has an invalid character in it (for example, if it was 123%7$), copy the string error into result.
originalNumber can contain numeric digits and upper or lower case letters (depending on the originalBase)
Your main should contain a loop that prompts uses to enter the original number and the two bases and then prints the conversion.
When you call FromBase, be sure the result is declared as a character array large enough to hold the largest possible result.
) For this exercise you will write a program to convert numbers to different bases. For example: Your program will prompt the user to enter a "number" and the base of the number. You will then ask what base to convert the number to. Your program will handle all bases 2-36 Your program should include the following features: If the user enters a base outside the range stated above, display an error message and make them re-enter the base. If the number doesn't match the base they typed in, display an error message and have them re-enter all of the information again. (i.e. The character A is invalid for a base 10 number.) The number displayed to the user should be displayed in uppercase. Be sure your output is self-documenting. For example: . . . FF (base 16) = 255 (base 10) Hints: The maximum digit that can be in a number is one less than its base. For example. 7 is the maximum digit in a base 8 number, 1 is the maximum digit in a base 2 number. Convert all numbers to base 10 first and then convert them to the desired base. Remember, since the number is entered into a character array, each digit is a character. You must convert this ASCII value to the appropriate value for that base. For example, A is value 10, B is value 11. To calculate these values use . ASCII value for the alphabetic character and subtract 55. The reason this works is A is ASCII value 65, B is 66. If the character in the array happens to be a digit i.e. number) use the same philosophy but subtract 48Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started