多学习。

csapp::tyype_pointerP31——将数值转换为十六进制

#include <stdio.h>
#include <stdlib.h>

//unsigned char恰好占用一字节,可以作为一个字节指针
typedef unsigned char *byte_pointer;

//若机器是小端机,则需要倒转着看.大端机顺着看即可
void show_bytes(byte_pointer start,size_t l)
{
    size_t i;
    for( i = 0; i < l; ++i)
        printf("%.2x ",start[i]);  //start[i]由于数组的封装相当于*(start+i)
    puts("");
}

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_val(int val)
{
    float fval = (float)val;
    int *p = &val;

    show_int(val);
    show_float(fval);
    show_pointer(p);
}

int main()
{
     test_show_val(12345)
;    return 0;
}

posted @   czyaaa  阅读(43)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示