In: Computer Science
Can I get some assistance with this request.
Right now the code that converts a hex character read on input into its decimal value is in the main routine. To make the main routine easier to read, make the conversion code concise and reusable, move it into a function named hex2Dec. The hex2Dec function should take a character parameter which is the character read from input. The function should return an integer which is the converted decimal value of the hexadecimal value represented by the character read from input and passed in as a parameter.
Tasks
Here is my project 1B. how can i change it to meet this requirement.
/******************************************************************************
Online C Compiler.
Code, Compile, Run and Debug C program online.
Write your code in this editor and press "Run" button to compile
and execute it.
*******************************************************************************/
#include <stdio.h>
int main(void) {
char input;
while (1){
scanf("%c", &input);
if (input == 'X')
{
break;}
printf("You have entered %c\n", input);
if (input >= '0' && input <= '9'){
int ascii_decimal = (int)input;
printf("Decimal value of the ASCII character %c =
%d\n", input, ascii_decimal);
int hexa = input - '0';
printf("Decimal value of the Hexadecimal digit %c =
%d\n", input, hexa);}
else{
int ascii_decimal = (int)input;
printf("Decimal value of the ASCII character %c =
%d\n", input, ascii_decimal);
int hexa = input - 'A' + 10;
printf("Decimal value of the Hexadecimal digit %c =
%d\n", input, hexa);}
if(input>='0'&&input<='9')
{
int ascii_decimal = (int) input;
printf("Decimal value of the ASCII character %c =
%d\n",input,ascii_decimal);
int hexa = input-'0';
printf("Decimal value of the Hexadecimal digit %c =
%d\n",input,hexa);}
else if(input>='A'&&input<='F')
{
int ascii_decimal = (int) input;
printf("Decimal value of the ASCII character %c =
%d\n",input,ascii_decimal);
int hexa = input-'A'+10;
printf("Decimal value of the Hexadecimal digit %c =
%d\n",input,hexa);}
else
{
printf("you have entered invalid input!!\n");}
fflush(stdin);}
return 0;
}
SOURCE CODE:
*Please follow the comments to better understand the code.
**Please look at the Screenshot below and use this code to copy-paste.
***The code in the below screenshot is neatly indented for better understanding.
/******************************************************************************
Online C Compiler.
Code, Compile, Run and Debug C program online.
Write your code in this editor and press "Run" button to compile
and execute it.
*******************************************************************************/
#include <stdio.h>
// Take the hexadecimal value and return the decimal
output.
int hex2Dec(char input)
{
printf("You have entered %c\n", input);
int ascii_decimal = (int)input;
printf("Decimal value of the ASCII character %c = %d\n", input,
ascii_decimal);
// if the character is a number 0-9
if (input >= '0' && input <= '9')
{
int hexa = input - '0';
return hexa;
}
else
{
// input is from (A-F)
int hexa = input - 'A' + 10;
return hexa;
}
}
int main(void)
{
char input;
while (1)
{
// read input character
scanf("%c", &input);
// if it's X, stop
if (input == 'X')
{
break;
}
// call the function here
int hexa=hex2Dec(input);
// print the output here.
printf("Decimal value of the Hexadecimal digit %c =
%d\n",input,hexa);
}
return 0;
}
===================
Screenshot:
OUTPUT: