In: Computer Science
Write a C Program to finish the following requirements: Convert a double number to its hexadecimal form
1. The double number x is located between 0 and 1000(0<=x<=1000),e.g.678.345
2.The double number with the hexadecimal form contains 6 significant digits. e.g." 5D.32FA45".
3.The double number with the hexadecimal form is represented by a string(or a character array), e.g."5D.32FA45".
Please write as simple as possible because I am a freshman in C!
Thanks!
A C program is given below that will take a number of type double between 0 and 1000 and converts the number in its equivalent hexadecimal form with six significant digits. The integer part and fraction part in the double number are converted separately. The whole procedure is shown in the program along with its description in the comments.
Program code:
/******************************************************************************
The program converts double number between 0 to 1000 to its hexadecimal form (upto 6 significant digits)
*******************************************************************************/
#include <stdio.h>
/*prototype for function intToHex*/
char intToHex(int);
/*main program starts*/
int main()
{
/*variables are declared*/
double d_value;
long i_part;
float f_part;
char hex[10]; /*the actual hex digits are stored in this array*/
char tempFHex[3]; /*temp storage for int part*/
int rem;
int i , j, counter;
printf("Enter double number (x): ");
scanf("%lf",&d_value);/*enter value from user*/
if(d_value >=0 && d_value <=1000) /*check whether the input number is in the given range [0,1000]*/
{
i_part = (int)d_value; /*getting the integer part*/
f_part = d_value - i_part; /*getting the fraction part*/
counter = 0; /* to maintain total characters in hex array*/
/* int part is converted into its equivalent hex form*/
while(i_part > 0)
{
rem = i_part%16;
i_part = i_part/16;
tempFHex[counter] = intToHex(rem);
counter++;
}
/*contents of tempFHex are stored into hex in reverse order*/
for(j = 0; j < counter; j++)
{
hex[j] = tempFHex[counter -j-1];
}
hex[counter]='.'; /*store the point*/
counter++;
/*The fraction Part is converted into its equivalent hex form and
this will be done for max 6 significant digits only*/
for(i = 1; i <=6 ;i++)
{
f_part = f_part * 16;
/*get int part and convert it to hex digit*/
i_part = (int)f_part;
hex[counter] = intToHex(i_part);
f_part = f_part - i_part;/*get next fraction part*/
counter++;
/*if at any point fraction part becomes zero
before 6th iteration , break out of the loop*/
if(f_part == 0){
break;
}
}
}
else{
/*the number is out of range*/
printf("The given number is out of the range [0,1000].");
return 0;
}
/*print the contents of hex array, which holds the hex form of input number*/
printf("\nThe hexadecimal number for the given double number is: ");
for(i = 0; i < counter ; i++)
{
printf("%c",hex[i]);
}
return 0;
}
/*function to convert num in 0 to 15 to its equivalent hex digit*/
char intToHex(int num)
{
char h_digit;
if(num >=0 && num <=15){
if(num >=0 && num<=9){
h_digit = num+'0';/*h_digit will be the num itself*/
}
else{
h_digit = 'A' + (num -10); /*10 will be 'A',11 will be 'B' ,....15 will be 'F'*/
}
}
/*printing final hexadecimal number*/
return h_digit;
}
Sample Outputs:
Output1: When number is within the range [0 to 1000]
Output2: When number is out of range [0 to 1000].