In this introductory minor assignment, you will write a complete C program that
ID: 3880594 • Letter: I
Question
In this introductory minor assignment, you will write a complete C program that will prompt for and accept two 4-digit hexadecimal numbers and then combine them (i.e not add them) into a single 32-bit number and print out the results. 1. Prompt for and read in two 4-digit hexadecimal numbers. You may assume that the user enters a hexadecimal number, though it may be out of range. A valid hexadecimal range is considered to be 0000 to FFFF. If out of range, you will continually repeat this process until two valid 4-digit hexadecimal numbers are entered Note that you may read in these two 4-digit hexadecimal numbers any way you ish (i.e., a hexadecimal number or a C-string that you convert to a hexadecimal number), but you must ultimately treat these as individual hexadecimal numbers. 2. You will print out the decimal representation for each of these hexadecimal numbers 3. You will concatenate the two 4-digit hexadecimal numbers as follows into a single 8-digit hexadecimal number, where the first hexadecimal number will be in the 4 least significant digits while the second hexadecimal number will be in the 4 most significant digits. Note that this operation to concatenate must be done numerically and not simply concatenated as a string. Ideally, you would use bitwise operators to do this, but either way, you must do so while treating these as hexadecimal numbers 4. You will print out the concatenated hexadecimal number as well as its decimal representation As a hint, you may want to perform bitwise operations to accomplish this, but also pay attention to the data types used for these numbers. Although not required, you may find Chapter 2 on Bits, Bytes, and Data Types in the System Programming with C and Unix optional reference textbook by Adam Hoover to be helpfulExplanation / Answer
here is your program :--------------------->>>>>>>>>>>>>>
#include<stdio.h>
#include<string.h>
#include<math.h>
long int convert(char *a,char *b){
strcat(b,a);
int i;
long int res = 0;
int c = 0;
for(i = strlen(b)-1;i>=0;i--){
if(b[i] == 'A'){
res += 10*(int)(pow(16,c));
}else if(b[i] == 'B'){
res += 11*(int)(pow(16,c));
}else if(b[i] == 'C'){
res += 12*(int)(pow(16,c));
}else if(b[i] == 'D'){
res += 13*(int)(pow(16,c));
}else if(b[i] == 'E'){
res += 14*(int)(pow(16,c));
}else if(b[i] == 'F'){
res += 15*(int)(pow(16,c));
}else{
res += (int)(b[i] - '0')*(int)(pow(16,c));
}
c++;
}
return res;
}
int main(){
char *a,*b;
a = (char *)malloc(sizeof(char)*5);
b = (char *)malloc(sizeof(char)*5);
int st = 0,i;
long int res= 0;
while(1){
printf(" Enter first 4-digit Hexadecimal Number :");
gets(a);
if(strlen(a) != 4){
printf(" Enter exactly 4-digit ");
continue;
}
st = 0;
for(i = 0;i<4;i++){
if(strchr("1234567890ABCDEF",a[i]) != NULL){
st++;
}
}
if(st == 4){
break;
}
}
while(1){
printf(" Enter Second 4-digit Hexadecimal Number :");
gets(b);
if(strlen(b) != 4){
printf(" Enter exactly 4-digit ");
continue;
}
st = 0;
for(i = 0;i<4;i++){
if(strchr("1234567890ABCDEF",b[i]) != NULL){
st++;
}
}
if(st == 4){
break;
}
}
res = convert(a,b);
printf("Hex : %s Decimal : %d ",b,res);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.