2018-2019-1 20165324 《信息安全系统设计基础》第三周课堂练习

本文详细介绍了通过编写C语言程序,展示学号的位序列,包括整数、浮点数和指针的内存布局,以及使用gdb进行调试的过程。实践内容涵盖了大端和小端字节序的判断,以及不同数据类型间的转换和表示。

2018-2019-1 20165324 《信息安全系统设计基础》第三周课堂练习

任务一
  1. 调用附图代码,编写一个程序“week0202学号.c",用show_int(),show_float()打印一下你的4位学号,参考教材P33打印出匹配的位序列。
  2. 提交运行结果截图,要全屏,要包含自己的学号信息
  3. 课下把代码推送到代码托管平台
  4. 参考教材p82,给出出匹配的位序列的推导过程
  5. 代码及截图如下:
#include <stdio.h>
typedef unsigned char *byte_pointer;
void show_bytes(byte_pointer start,size_t len)
{
    size_t i;
    for (i=0;i<len;i++)
        printf("%.2x",start[i]);
    printf("\n");
}
void show_int (int x)
{
    show_bytes((byte_pointer) &x,sizeof(int));
}
void show_float(float x)
{
    show_bytes((byte_pointer) &x,sizeof(float));
}
void show_pointer(void *x)
{
    show_bytes((byte_pointer) &x,sizeof(void *));
}
void test_show_bytes(int val)
{
    int ival=val;
    float fval=(float)val;
    int  *pval = &ival;
    show_int(ival);
    show_float(fval);
    show_pointer(pval);
}
main()
{
char c=0x12345678;
show_int(c);
if(c==0x12)
printf("20155337是大端\n");
else
printf("20155337是小端\n");

1290756-20181013221137473-1419760524.png

实践二
  1. 调用附图代码,编写一个程序 “week0602学号.c",用show_int(), show_float()打印一下你的4位学号,参考教材P33打印出匹配的位序列。
  2. 提交运行结果截图,要全屏,要包含自己的学号信息
  3. 代码及截图如下:
#include <stdio.h>
typedef unsigned char *byte_pointer;
void show_bytes(byte_pointer start,size_t len)
{
    size_t i;
    for (i=0;i<len;i++)
        printf("%.2x",start[i]);
    printf("\n");
}
void show_int (int x)
{
    show_bytes((byte_pointer) &x,sizeof(int));
}
void show_float(float x)
{
    show_bytes((byte_pointer) &x,sizeof(float));
}
void show_pointer(void *x)
{
    show_bytes((byte_pointer) &x,sizeof(void *));
}
void test_show_bytes(int val)
{
    int ival=val;
    float fval=(float)val;
    int  *pval = &ival;
    show_int(ival);
    show_float(fval);
    show_pointer(pval);
}
int main()
{
    int x=5337;
    test_show_bytes(x);
}

1290756-20181013221151542-1757267961.png

任务三
  1. 编写一个程序 “week0203学号.c",运行下面代码:
1   short int v = -学号后四位
2   unsigned short uv = (unsigned short) v
3  printf("v = %d,  uv = %u\n ", v, uv);
  1. 在第三行设置断点用gdb调试,用p /x v; p /x uv 查看变量的值,提交调试结果截图,要全屏,要包含自己的学号信息

  2. 分析p /x v; p /x uv 与程序运行结果的不同和联系
  3. 代码及截图如下:
#include <stdio.h>
typedef unsigned char *byte_pointer;
void show_bytes(byte_pointer start,size_t len)
{
    size_t i;
    for (i=0;i<len;i++)
        printf("%.2x",start[i]);
    printf("\n");
}
void show_int (int x)
{
    show_bytes((byte_pointer) &x,sizeof(int));
}
void show_float(float x)
{
    show_bytes((byte_pointer) &x,sizeof(float));
}
void show_pointer(void *x)
{
    show_bytes((byte_pointer) &x,sizeof(void *));
}
void main()
{
short int v = -学号后四位

unsigned short uv = (unsigned short) v

printf("v = %d, uv = %u\n ", v, uv);
}

转载于:https://www.cnblogs.com/20165324hcj/p/9771537.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值