永发信息网

请C语言高手来做一做《深入理解计算机系统》里面关于字节替换的习题。

答案:1  悬赏:60  手机版
解决时间 2021-04-04 23:21
  • 提问者网友:兔牙战士
  • 2021-04-04 09:10
请C语言高手来做一做《深入理解计算机系统》里面关于字节替换的习题。
最佳答案
  • 五星知识达人网友:酒者煙囻
  • 2021-04-04 10:03
OK了
#include
#include
typedef unsigned char *byte_pointer;
void show_bytes(unsigned output)
{
int i,temp;
int len=0;
char index[16]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
char *res=(char*)malloc(30);
printf("0x");

while (output!=0)
{
*res++=index[output%16];
len++;
output/=16;
}

res--;

for (i=len-1;i>=0;i--)
{
printf("%c",*res--);
}
printf("\n");
}
unsigned put_byte (unsigned x, unsigned char b, int i)
{
unsigned *px;
unsigned char *p;
px=&x;
p=(unsigned char *)px;
*(p++)=b;
return x;
}

int main()
{
unsigned input=0x12345678;
unsigned output=put_byte (input, 0xAB, 1);
printf("0x%x\n",output);
show_bytes(output);
return 0;
}追问你把重点弄错了,关键是改put_byte的部分,比如我把main里的实参1改成2或3显示都和题目的预期结果不一样,show_bytes是书中有的代码。

我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯