show_bytes函数

show_bytes打印出每个以十六进制表示的字节

  • 这段代码使用强制转换来访问和打印不同程序对象的字节表示

  • 用typedef将数据结构类型byte_pointer定义为一个指向类型“unsigned char”的对象的指针。

    typedef unsigned char *byte_pointer;

  • 引用一个字节序列,这样一个字节指针其中每个字节都被认为是一个非负整数。第一个例程show_bytes的输入是一个字节序列的地址。他用一个字结指针以及一个字节数来表示。该字节数指定为数据类型size_t,表示数据结构大小的首选数据类型。C格式化指令“%.2x”表明整数必须使用至少两个数字的十六进制格式输出。

    void show_bytes(byte_pointer start, size_t len)
    {
    size_t i;
    for (i = 0; i < len; i++)
    printf("%p\t0x%.2x\n", &start[i], start[i]);
    printf("\n");
    }

  • 过程show_int、show_float、和show_pointer展示了如何实用程序show_bytes来分别输出类型为int、float、和void *的C程序对象的字节表示。

    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 *));
    }

(1)这三段程序仅仅传递给show_bytes一个指向它们参数x的指针&x,且这个指针别强制类型转换 成“unsigned char * ”。这个强制类型转换告诉我们编译器,程序应该把这个指针看成指向一个字节序列,而不是指向一个原始数据类型的对象。这个值真会被看成是对象使用的最低字节地址。
(2)使用C语言的运算符sizeof来确定对象使用的字节数,一般来说,表达式sizeof(T)返回存储一个类型为T的对象所需要的字节数,使用sizeof而不是一个固定的值,是向编写在不同机器类型上可移植的代码迈进了一步。

测试机器是大端模式还是小端模式

void test_show_bytes(int val) {
int ival = val;
//float fval = (float) ival;
double fval = (double) ival;
int *pval = &ival;
printf(“Stack variable ival = %d\n”, ival);
printf("(int)ival:\n");
show_int(ival);
printf("(float)ival:\n");
show_float(fval);
printf("&ival:\n");
show_pointer(pval);
}

输入参数12345
gcc编译后的结果
参数12345的十六进制表示为ox00003039.在我的电脑上,最低有效字节ox39最先输出,这说明我的机器是小端法机器。同样地,float型和指针型数据也是如此。

#include <stdio.h>
#include <stdlib.h>
#include <string.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("%p\t0x%.2x\n", &start[i], 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) ival;
 double fval = (double) ival;
    int *pval = &ival;
    printf("Stack variable ival = %d\n", ival);
    printf("(int)ival:\n");
    show_int(ival);
    printf("(float)ival:\n");
    show_float(fval);
    printf("&ival:\n");
    show_pointer(pval);
}
void simple_show_a() {
/* $begin simple-show-a */
int val = 0x87654321;
byte_pointer valp = (byte_pointer) &val;
show_bytes(valp, 1); /* A. */
show_bytes(valp, 2); /* B. */
show_bytes(valp, 3); /* C. */
/* $end simple-show-a */
}
void simple_show_b() {
/* $begin simple-show-b */
int val = 0x12345678;
byte_pointer valp = (byte_pointer) &val;
show_bytes(valp, 1); /* A. */
show_bytes(valp, 2); /* B. */
show_bytes(valp, 3); /* C. */
/* $end simple-show-b */
}
void float_eg() {
  int x = 3490593;
  float f = (float) x;
  printf("For x = %d\n", x);
  show_int(x);
  show_float(f);
  x = 3510593;
  f = (float) x;
  printf("For x = %d\n", x);
  show_int(x);
  show_float(f);
}
void string_ueg() {
/* $begin show-ustring */
const char *s = "ABCDEF";
show_bytes((byte_pointer) s, strlen(s)); 
/* $end show-ustring */
}
void string_leg() {
/* $begin show-lstring */
const char *s = "abcdef";
show_bytes((byte_pointer) s, strlen(s)); 
/* $end show-lstring */
}
void show_twocomp() 
{
/* $begin show-twocomp */
    short x = 12345; 
    short mx = -x; 
    show_bytes((byte_pointer) &x, sizeof(short)); 
    show_bytes((byte_pointer) &mx, sizeof(short)); 
/* $end show-twocomp */
}
int main(int argc, char *argv[])
{
    int val = 12345;
    if (argc > 1) {
        val = strtol(argv[1], NULL, 0);
 printf("calling test_show_bytes\n");
 test_show_bytes(val);
    } else {
 printf("calling show_twocomp\n");
 show_twocomp();
 printf("Calling simple_show_a\n");
 simple_show_a();
 printf("Calling simple_show_b\n");
 simple_show_b();
 printf("Calling float_eg\n");
 float_eg();
 printf("Calling string_ueg\n");
 string_ueg();
 printf("Calling string_leg\n");
 string_leg();
    }
    return 0;
}
  • 8
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
代码存在的逻辑性问题有: 1. 在函数 clip_1_1 和 clip_4_3 中,进行宽高的重新赋值操作,但是并没有对 n_bytes 进行调整,导致输出的图片信息不准确。 2. 在函数 clip_4_3 中,对于宽高比例不符合 4:3 的情况,使用了嵌套的 if-else 结构,使得代码的逻辑复杂,容易出错。同时,在进行浮点数运算时,使用了整数除法,导致计算结果不准确。 3. 在 main 函数中,多次使用了相同的变量名,容易引起混淆。 针对这些问题,可以进行如下的改进: 1. 在进行宽高赋值操作时,同时对 n_bytes 进行调整。 2. 在函数 clip_4_3 中,可以使用简单的 if-else 结构,避免嵌套和复杂的计算。同时,可以将浮点数计算转换为整数计算,避免精度问题。 3. 修改 main 函数中的变量名,使其更具有描述性。 改进后的代码如下: ```c++ #include<bits/stdc++.h> using namespace std; class Photo { private: int width; int height; int n_bytes; public: static int num; Photo():width(0),height(0),n_bytes(0){ num++; }; Photo(int w, int h, int n):width(w),height(h),n_bytes(n){ num++; }; Photo(const Photo& p){ this->width = p.width; this->height = p.height; this->n_bytes = p.n_bytes; num++; }; void show_info(){ cout << num << " " << width << " " << height << " " << n_bytes << endl; };//显示图片信息,输出格式见样例 void clip_1_1(){ if(width > height){ height = width; width = height * 1.0 / 1.0 + 0.5; n_bytes = n_bytes * width * height / (width + height) / (width + height); }else{ width = height; height = width * 1.0 / 1.0 + 0.5; n_bytes = n_bytes * width * height / (width + height) / (width + height); } };//将图片按最大的正方形裁剪,计算新的高和宽。结果截断小数部分,下同 void clip_4_3(){ if(width * 3 > height * 4){ height = width * 4 / 3; n_bytes = n_bytes * width * height / (width + height) / (width + height); }else{ width = height * 4 / 3; n_bytes = n_bytes * width * height / (width + height) / (width + height); } };//将图片按最大的4:3形状裁剪,计算新的高和宽 }; int Photo::num = 0; int main(){ Photo photo1(4000, 3000, 4); photo1.clip_1_1(); photo1.show_info(); Photo photo2(4000, 3000, 4); photo2.clip_4_3(); photo2.show_info(); Photo photo3(photo2); photo3.clip_1_1(); photo3.show_info(); int input_height, input_width, input_n_bytes; cin >> input_height >> input_width >> input_n_bytes; Photo photo4(input_height, input_width, input_n_bytes); photo4.clip_4_3(); photo4.show_info(); photo4.clip_1_1(); photo4.show_info(); return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值