show_bytes函数

博客介绍了show_bytes函数,它能打印以十六进制表示的字节,通过强制转换访问和打印不同程序对象的字节表示。还给出了show_int、show_float和show_pointer等函数示例。此外,通过测试程序判断机器是大端还是小端模式,以参数12345测试表明机器为小端法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值