Discover a wealth of knowledge at Westonci.ca, where experts provide answers to your most pressing questions. Get quick and reliable answers to your questions from a dedicated community of professionals on our platform. Get immediate and reliable solutions to your questions from a community of experienced professionals on our platform.

Write a C program that reads two hexadecimal values from the keyboard and then stores the two values into two variables of type unsigned char. Read two int values p and k from the keyboard, where the values are less than 8. Replace the n bits of the first variable starting at position p with the last n bits of the second variable. The rest of the bits of the first variable remain unchanged. Display the resulting value of the first variable using printf %x.Test Cases:n = 3;p=4;input: a= 0x1f (0001 1111) b = c3 (1100 0011); output: f (0000 1111)n = 2;p=5;input: a= 0x1f (0001 1111) b = c3 (1100 0011); output: 3f (0011 1111)

Sagot :

Solution :

#include  [tex]${data-answer}lt;\text{stdio.h}>$[/tex]

#include [tex]${data-answer}lt;\text{string.h}>$[/tex]

#include [tex]${data-answer}lt;\text{stdlib.h}>$[/tex]

//Converts [tex]$\text{hex string}$[/tex] to binary string.

[tex]$\text{char}$[/tex] * hexadecimal[tex]$\text{To}$[/tex]Binary(char* hexdec)

{

 

long [tex]$\text{int i}$[/tex] = 0;

char *string = [tex]$(\text{char}^ *) \ \text{malloc}$[/tex](sizeof(char) * 9);

while (hexdec[i]) {

//Simply assign binary string for each hex char.

switch (hexdec[i]) {

[tex]$\text{case '0'}:$[/tex]

strcat(string, "0000");

break;

[tex]$\text{case '1'}:$[/tex]

strcat(string, "0001");

break;

[tex]$\text{case '2'}:$[/tex]

strcat(string, "0010");

break;

[tex]$\text{case '3'}:$[/tex]

strcat(string, "0011");

break;

[tex]$\text{case '4'}:$[/tex]

strcat(string, "0100");

break;

[tex]$\text{case '5'}:$[/tex]

strcat(string, "0101");

break;

[tex]$\text{case '6'}:$[/tex]

strcat(string, "0110");

break;

[tex]$\text{case '7'}:$[/tex]

strcat(string, "0111");

break;

[tex]$\text{case '8'}:$[/tex]

strcat(string, "1000");

break;

[tex]$\text{case '9'}:$[/tex]

strcat(string, "1001");

break;

case 'A':

case 'a':

strcat(string, "1010");

break;

case 'B':

case 'b':

strcat(string, "1011");

break;

case 'C':

case 'c':

strcat(string, "1100");

break;

case 'D':

case 'd':

strcat(string, "1101");

break;

case 'E':

case 'e':

strcat(string, "1110");

break;

case 'F':

case 'f':

strcat(string, "1111");

break;

default:

printf("\nInvalid hexadecimal digit %c",

hexdec[i]);

string="-1" ;

}

i++;

}

return string;

}

 

int main()

{ //Take 2 strings

char *str1 =hexadecimalToBinary("FA") ;

char *str2 =hexadecimalToBinary("12") ;

//Input 2 numbers p and n.

int p,n;

scanf("%d",&p);

scanf("%d",&n);

//keep j as length of str2

int j=strlen(str2),i;

//Now replace n digits after p of str1

for(i=0;i<n;i++){

str1[p+i]=str2[j-1-i];

}

//Now, i have used c library strtol

long ans = strtol(str1, NULL, 2);

//print result.

printf("%lx",ans);

return 0;

}