深入理解计算机系统基础 show_bytes函数

show_bytes:打印任意类型数据的十六进制表示

一. 源代码部分

#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;
    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() {
    int val = 0x87654321;
    byte_pointer valp = (byte_pointer) &val;
    show_bytes(valp, 1); /* A. */     
    show_bytes(valp, 2); /* B. */
    show_bytes(valp, 3); /* C. */
}
void 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. */
}
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() {
    const char *s = "ABCDEF";
    show_bytes((byte_pointer) s, strlen(s));
}
void string_leg() {
    const char *s = "abcdef";
    show_bytes((byte_pointer) s, strlen(s));
}
void show_twocomp()
{
    short x = 12345; 
    short mx = -x;
    show_bytes((byte_pointer) &x, sizeof(short));
    show_bytes((byte_pointer) &mx, sizeof(short));
}
int main(int argc, char *argv[])
{
    int val = 12345;
    if (argc > 1) {
    val = strtol(argv[1], NULL, 0);    //转换为long型
    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;
}

二. 运行结果

  • 不带参数下的运行结果
    在这里插入图片描述
    在这里插入图片描述在这里插入图片描述

三. 代码解析

#include <stdlib.h>

  • stdlib 头文件里包含了C、C++语言的最常用的系统函数,该文件包含了的C语言标准库函数的定义。stdlib.h里面定义了五种类型、一些宏和通用工具函数。类型例如size_t、wchar_t、div_t、ldiv_t和lldiv_t; 宏例如EXIT_FAILURE、EXIT_SUCCESS、RAND_MAX和MB_CUR_MAX等等; 常用的函数如malloc()、calloc()、realloc()、free()、system()、atoi()、atol()、rand()、srand()、exit()等等。
  • show_bytes中用到了size_t 。
  • size_t是unsigned int型。

typedef unsigned char *byte_pointer;
//typedef char *byte_pointer;
//typedef int *byte_pointer;

  • 这里用unsigned char * 类型而不用char * 或者int * ,是因为unsigned char * 是无符号类型,它的符号位不受影响,因此可以确保运行时不会超过值得范围而溢出。但char * 、int * 是有符号类型,要考虑它们的符号位,如果值超过范围将会扩展导致溢出,最终导致出现错误的运行结果。

printf("%p\t0x%.2x\n", &start[i], start[i]);

  • %p表示输出指针的值。
  • \t 为转义字符,表示跳到下一个Tab位置。
  • %.2x表示整数必须使用至少两个数字的十六进制格式输出

void show_pointer(void *x)

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

  • 该函数测试机器是大端模式还是小端模式。
  • 不可以写成float fval = (float) ival的原因是 int转换成float不会发生溢出,但可能会被舍人。

void show_twocomp()
{
short x = 12345;
short mx = -x;
show_bytes((byte_pointer) &x, sizeof(short));
show_bytes((byte_pointer) &mx, sizeof(short));
}

  • 小端模式下:12345=0x00003039
  • 小端模式下:-12345=0x0000cfc7
  • 5
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Linux内核中,`DEFINE_SHOW_ATTRIBUTE`函数用于定义一组`show`函数和`store`函数,以便用户空间可以使用sysfs文件系统来访问内核中的某些属性。具体来说,`DEFINE_SHOW_ATTRIBUTE`函数接受三个参数:属性名、`show`函数和`store`函数。其中,`show`函数用于读取属性值,并将其输出到缓冲区中;`store`函数用于将用户空间传递的新属性值写入内核中。 举例来说,下面是一个使用`DEFINE_SHOW_ATTRIBUTE`函数定义`sysfs`属性的示例: ```c static ssize_t my_attr_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) { return sprintf(buf, "Hello, world!\n"); } static ssize_t my_attr_store(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t count) { // Do something with the new attribute value return count; } static struct kobj_attribute my_attribute = __ATTR(my_attr, 0666, my_attr_show, my_attr_store); static struct attribute *attrs[] = { &my_attribute.attr, NULL, }; static struct attribute_group attr_group = { .attrs = attrs, }; static struct kobject *my_kobj; static int __init my_module_init(void) { int ret; my_kobj = kobject_create_and_add("my_kobject", kernel_kobj); if (!my_kobj) { return -ENOMEM; } ret = sysfs_create_group(my_kobj, &attr_group); if (ret) { kobject_put(my_kobj); return ret; } return 0; } static void __exit my_module_exit(void) { sysfs_remove_group(my_kobj, &attr_group); kobject_put(my_kobj); } module_init(my_module_init); module_exit(my_module_exit); ``` 在上述示例中,我们定义了一个名为`my_attr`的属性,并将其加入到一个名为`my_kobject`的内核对象中。`my_attr_show`函数用于读取属性值,在本例中返回值为`Hello, world!\n`。`my_attr_store`函数用于将用户空间传递的新属性值写入内核中,在本例中只是简单地返回传入的属性值。最后,我们通过`sysfs_create_group`函数将这个属性加入到`my_kobject`的属性组中,以便用户空间可以使用sysfs文件系统来访问它。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值