In this project, we convert a positive decimal number to its positive hexadecimal value equivalent and...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
In this project, we convert a positive decimal number to its positive hexadecimal value equivalent and vice versa. The code should prompt the user for a choice of 1 or 2: If the user enters 1 it means the user wants to convert a decimal number to its hexadecimal value equivalent. If the user enters 2 it means the user wants to convert a hexadecimal number to its decimal value equivalent Since the hexadecimal numbers include characters between A-F we have to keep a hexadecimal number as an array of characters. To read a hexadecimal number, we need to read a string. To keep this code simple, just enter and display capital letters for hexadecimal numbers. Your code needs to include at least two functions. One function to convert a decimal number to its hexadecimal value equivalent and prints both values. The other function to convert a hexadecimal number to its decimal value equivalent and prints both values. Input and output samples: Input values are bolded. Sample1: Enter 1 for Decimal to Hexadecimal conversion. Enter 2 for Hexadecimal to Decimal conversion. 1 Enter a positive decimal number. 255 The hexadecimal representation of 0 is FF. Sample 2: Enter 1 for Decimal to Hexadecimal conversion. 2 Enter 2 for Hexadecimal to Decimal conversion. FF Enter a positive hexadecimal number. The hexadecimal representation of FF is 255. Hint: You can do this project your way, but I provide some hints here. 1. //To Do Open needed libraries 2. To Do: Declare two functions as you see in this code. int main() { 3. //To Do: Declare an int named dec. 4. //To Do: Declare an int named choice. 5. //To Do: Declare an array of size 12 of characters named hex. } 6. printf("Enter 1 for Decimal to Hexadecimal conversion. "); printf("Enter 2 for Hexadecimal to Decimal conversion. "); // To Do choice. - if (choice == 1) { 7. Get a number from the user and save it in printf("Enter a positive decimal number. "); // To Do Get a decimal value number from the user and save it in dec. 8. // To Do - Call dectohex function. } else{ printf("Enter a positive hexadecimal number. "); 9. - // To Do Get a hexadecimal number as a string from the user and save it in hex. 10. // To Do - Call hextodec function } return 0; void dectohex (int x) { int r = 0; int i; char arr [32]; 11. //To Do Initial all elements of arr with character ' ' //Don't forget to put '\0' in the last element of the array. while (x!=0) { 12. // To Do Refer to more details after code. } printf("The hexadecimal representation of %d is ", x ); puts (arr); return; } void hextodec (char *str). { int size = strlen(str); int i = 0; int dec = 0; char temp; for (i=0; i < size; i++) { 13. // To Do Refer to more details after code. } printf("The hexadecimal representation of %s is %d. ", str, dec); return; } 1. dectohex: Line #12 a. Find the remainder of x/16 and save it to the array and continue with the quotient till you reach 0. Pay attention that you find the least significant digit first and you need to save it to the last element of the array. Therefore, populate your array from right to left. b. Hexadecimal numbers use 0-9 and A-F. The remainder of x/16 is a number between 0-15. Since the array is an array of characters, we need some characters to represent 0-15. i. Look at the following table to find the relation between digit 0 and character '0'. Please do not include many if-else conditions to convert a number to a digit. You can find a pattern. 0 -> '0' 1-> '1' : : 9-> '9'. ii. To convert a number greater than 9 to a hexadecimal digit use the ASCCI code of 'A' and add the remainder -10 to it. 10-> 'A' 'A' +0 11 'B' 'A' +1 12 'C' 'A' +2 : 15 'F' 'A' +5 2. hextodec: Line # 13 To convert a hex number to its decimal value, you can start from the leftmost digit, multiply it by 16 and add it to the following digit. B3E4 =(((((B*16)+3)*16)+E)*16+4) = 46068. a. All the values that are saved in str are characters. Therefore, if you want to convert B3E4 to its decimal value, you need to consider each digit as a character. b. Look at the following example to convert a hexadecimal number to a decimal number. (0*16) 'B' (0*16) + 11 = 11 (11 *16) '3' + = (11 *16) + 3 = 179 (179 *16) + 'F' = (179 *16) + 15 = 2879 (2879 *16)+'4' (2879 *16) + 4 = 46068 The hexadecimal representation of B3F4 is 46068. c. To convert a hex digit to a decimal digit if the hex digit is '0' to '9' just subtract the ASCII code of '0'. d. To convert a hex digit to a decimal digit if the hex digit is 'A' to 'F' just subtract the ASCII code of 'A'. In this project, we convert a positive decimal number to its positive hexadecimal value equivalent and vice versa. The code should prompt the user for a choice of 1 or 2: If the user enters 1 it means the user wants to convert a decimal number to its hexadecimal value equivalent. If the user enters 2 it means the user wants to convert a hexadecimal number to its decimal value equivalent Since the hexadecimal numbers include characters between A-F we have to keep a hexadecimal number as an array of characters. To read a hexadecimal number, we need to read a string. To keep this code simple, just enter and display capital letters for hexadecimal numbers. Your code needs to include at least two functions. One function to convert a decimal number to its hexadecimal value equivalent and prints both values. The other function to convert a hexadecimal number to its decimal value equivalent and prints both values. Input and output samples: Input values are bolded. Sample1: Enter 1 for Decimal to Hexadecimal conversion. Enter 2 for Hexadecimal to Decimal conversion. 1 Enter a positive decimal number. 255 The hexadecimal representation of 0 is FF. Sample 2: Enter 1 for Decimal to Hexadecimal conversion. 2 Enter 2 for Hexadecimal to Decimal conversion. FF Enter a positive hexadecimal number. The hexadecimal representation of FF is 255. Hint: You can do this project your way, but I provide some hints here. 1. //To Do Open needed libraries 2. To Do: Declare two functions as you see in this code. int main() { 3. //To Do: Declare an int named dec. 4. //To Do: Declare an int named choice. 5. //To Do: Declare an array of size 12 of characters named hex. } 6. printf("Enter 1 for Decimal to Hexadecimal conversion. "); printf("Enter 2 for Hexadecimal to Decimal conversion. "); // To Do choice. - if (choice == 1) { 7. Get a number from the user and save it in printf("Enter a positive decimal number. "); // To Do Get a decimal value number from the user and save it in dec. 8. // To Do - Call dectohex function. } else{ printf("Enter a positive hexadecimal number. "); 9. - // To Do Get a hexadecimal number as a string from the user and save it in hex. 10. // To Do - Call hextodec function } return 0; void dectohex (int x) { int r = 0; int i; char arr [32]; 11. //To Do Initial all elements of arr with character ' ' //Don't forget to put '\0' in the last element of the array. while (x!=0) { 12. // To Do Refer to more details after code. } printf("The hexadecimal representation of %d is ", x ); puts (arr); return; } void hextodec (char *str). { int size = strlen(str); int i = 0; int dec = 0; char temp; for (i=0; i < size; i++) { 13. // To Do Refer to more details after code. } printf("The hexadecimal representation of %s is %d. ", str, dec); return; } 1. dectohex: Line #12 a. Find the remainder of x/16 and save it to the array and continue with the quotient till you reach 0. Pay attention that you find the least significant digit first and you need to save it to the last element of the array. Therefore, populate your array from right to left. b. Hexadecimal numbers use 0-9 and A-F. The remainder of x/16 is a number between 0-15. Since the array is an array of characters, we need some characters to represent 0-15. i. Look at the following table to find the relation between digit 0 and character '0'. Please do not include many if-else conditions to convert a number to a digit. You can find a pattern. 0 -> '0' 1-> '1' : : 9-> '9'. ii. To convert a number greater than 9 to a hexadecimal digit use the ASCCI code of 'A' and add the remainder -10 to it. 10-> 'A' 'A' +0 11 'B' 'A' +1 12 'C' 'A' +2 : 15 'F' 'A' +5 2. hextodec: Line # 13 To convert a hex number to its decimal value, you can start from the leftmost digit, multiply it by 16 and add it to the following digit. B3E4 =(((((B*16)+3)*16)+E)*16+4) = 46068. a. All the values that are saved in str are characters. Therefore, if you want to convert B3E4 to its decimal value, you need to consider each digit as a character. b. Look at the following example to convert a hexadecimal number to a decimal number. (0*16) 'B' (0*16) + 11 = 11 (11 *16) '3' + = (11 *16) + 3 = 179 (179 *16) + 'F' = (179 *16) + 15 = 2879 (2879 *16)+'4' (2879 *16) + 4 = 46068 The hexadecimal representation of B3F4 is 46068. c. To convert a hex digit to a decimal digit if the hex digit is '0' to '9' just subtract the ASCII code of '0'. d. To convert a hex digit to a decimal digit if the hex digit is 'A' to 'F' just subtract the ASCII code of 'A'.
Expert Answer:
Posted Date:
Students also viewed these programming questions
-
Investment Portfolio and Risk diversification. Analyze the situations that arise in each of the problems and calculate the value or values ?? required for each of these. Problem 2: List and discuss...
-
Explain why increasing the normal anisotropy, R, of a sheet metal improves its deep drawability.
-
What are the six steps in determining unit product costs in a process costing system?
-
Decentralization and Transfer Pricing The city of Medina Park operates a plumbing and electrical maintenance department, responsible for maintaining all water and electric service functions in...
-
Ehrhart Appliance uses a perpetual inventory system. For its fat-screen television sets, the January 1 inventory was 3 sets at $600 each. On January 10, Ehrhart purchased 6 units at $660 each. The...
-
Required: An auditor is trying to figure out if the inventory at an electronics store is obsolete. From the list below, identify whether each attribute would be useful for predicting inventory...
-
Required information [The following information applies to the questions displayed below.] XYZ Corporation has a deferred compensation plan under which it allows certain employees to defer up to 40...
-
In (3x+7) Find the slope of f(x) = at (6, 1.58). Use a graphing utility and round to the nearest hundredth. 3
-
How can collaboration and communication tools integrated into an information system enhance the teamwork and productivity of laboratory project participants?
-
A spring scale hung from the ceiling stretches by 6.0 cm when a 1.4 kg mass is hung from it. The 1.4 kg mass is removed and replaced with a 2.0 kg mass. 4 Part A What is the stretch of the spring?...
-
How would you measure the success of your leadership in improving team productivity and motivation at a Company?
-
Calculate gross pay for each of the following employees. All are paid an overtime wage rate that is 1.5 times their respective regular wage rates. NOTE: For simplicity, all calculations throughout...
-
An 1) What could lead to if in a poor Project management in spite of great Portfolio management? (10 marks) 2) What are the key processes in scope management? (10 marks) 3) Which kind of project life...
-
What are three disadvantages of using the direct write-off method?
-
Determine which of -6 and -2 is larger, and express that using both the greater than and the less than notations.
-
Calculate \(|19|\).
-
Use your calculator to calculate 27 - (-13). Explain how the answer agrees with what was expected.
![Mobile App Logo](https://dsd5zvtm8ll6.cloudfront.net/includes/images/mobile/finalLogo.png)
Study smarter with the SolutionInn App