Your program will ask the user for an integer and then display the hex (i.e. bas
ID: 3568182 • Letter: Y
Question
Your program will ask the user for an integer and then display the hex (i.e. base 16) representation of the number. It will do the conversion using shift and masking operations.
Here is suggested sample run of your program:
./lab7.exe
Please input a decimal integer: 90241
Here is your integer in hexadecimal notation: 0x00016081
Would you like to input another integer? (y/n): y
Please input a decimal integer: 12728897
Here is your integer in hexadecimal notation: 0x00c23a41
Would you like to input another integer? (y/n): y
Please input a decimal integer: 40028926
Here is your integer in hexadecimal notation: 0x0262cafe
Would you like to input another integer? (y/n): n
Thanks for using the CS 262 Hexadecimal converter!
Goodbye!!!!
General Instructions
Your program will have a function char *to_hex(int n) which will take an int argument and return a string with the hex representation of the argument. Your main() function will prompt the user for an integer, accept the integer from the keyboard, pass it to to_hex() snd display the returned string. For this lab, you will need to use separate compilation and prepare a makefile to compile the separate files. To do this, you will need to place your main() function in an application file, your to_hex() function in an implementation file, and construct an interface (or header) file to #include in both application and implementation files. When submitting on Blackboard, all four files should be attached to your submission.
Implementation Specifications
The function to_hex() will determine the characters to put in the constructed string by reading the first (high-order) four bits (enough for one hex digit) from the argument passed into the function. (Remember to use lower case letters 'a' through 'f' to correspond to hex values 10 through 15.) The hex value should be saved in a cstring as character data. The function must then shift the original argument n four bits left to determine the next hex digit, and so on.
Here are the basic steps to_hex() will need to perform:
{ Declare a dynamic cstring that is large enough to hold the string of hex digits for one integer. This may be represented by: (sizeof(int)*2 + 3) * sizeof(char))
Put the characters "0x" in the first two positions of your cstring.
For each byte of the integer * 2 (in other words, sizeof(int)*2 times) // because each byte of the integer will be represented by two hex characters
{
copy the argument n to a new temporary integer variable
shift your variable right enough bits to leave the requisite four bits in the low-order bit positions (this will vary depending upon which set of bits you are working with)
use the bitwise AND operator and a bit mask to clear all but the low-order four bits, leaving a value in the range 0 to 15
if the result is < 10, store the appropriate character '0' through '9' in the next available place in your character array
else store the appropriate character 'a' through 'f' in the next spot of the array
}
Store the NULL terminator character at the end of your hex string
Return a pointer to the character array
}
Please note: You should not use "32" (or any other integer literal) when representing the number of bits in an integer. You cannot be sure that the system you are using has 32-bit integers, and therefore you should always use sizeof() to compute the size of an integer on the particular system you are on.
Explanation / Answer
#include <stdio.h>
#include <math.h>
#include <string.h>
void decimal_hex(int n, char hex[]);
int hex_decimal(char hex[]);
int main()
{
char hex[20];
int n;
printf("Instructions: ");
printf("Enter decimal number: ");
scanf("%d",&n);
decimal_hex(n,hex);
printf("Hexadecimal number: %s",hex);
}
if (c=='d' || c=='D')
{
printf("Enter hexadecimal number: ");
scanf("%s",hex);
printf("Decimal number: %d",hex_decimal(hex));
}
return 0;
}
void decimal_hex(int n, char hex[]) /* Function to convert decimal to hexadecimal. */
{
int i=0,rem;
while (n!=0)
{
rem=n%16;
switch(rem)
{
case 10:
hex[i]='A';
break;
case 11:
hex[i]='B';
break;
case 12:
hex[i]='C';
break;
case 13:
hex[i]='D';
break;
case 14:
hex[i]='E';
break;
case 15:
hex[i]='F';
break;
default:
hex[i]=rem+'0';
break;
}
++i;
n/=16;
}
hex[i]='';
strrev(hex); /* Reverse string */
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.