2822. 内存显示

BillChen2000

用上了CSAPP里的bytepointer的头文件?

// bytepointer.h
#include <stdio.h>
typedef unsigned char *byte_pointer;
void show_bytes(byte_pointer start, size_t len){
    int i;
    for (i = 0; i < len; i++)
        printf("%.2x ", start[i]);
}
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_double(float x){
    show_bytes((byte_pointer)&x, sizeof(double));
}
Andrew-Malcom

include

using namespace std;
int main()
{
        char str[101];
        while(scanf("%s",str)!=EOF){
                int flag=0;
                for(int i=0;i<strlen(str);i++){
                        if(str[i]=='.') flag=1;
                }
                if(flag==1){
                        double n=atof(str);
                        int p=sizeof(n);
                        unsigned char *e=(unsigned char *)&n;
                        while(p--){
                                printf("%02x ",*e++);
                        }
                        printf("\n");
                }
                else if(flag==0){
                        int n=atoi(str);
                        int p=sizeof(n);
                        unsigned char*e=(unsigned char *)&n;
                        while(p--){
                                printf("%02x ",*e++);
                        }
                        printf("\n");
                }
        }
}
ClassNotFoundException

printf(“%02x”,*p);小写x

10152130123

2822
[em:01][em:01][em:01]给楼主一个大大的赞!

clysto
#include <bits/stdc++.h>
#include <cstdio>

using namespace std;

int main() {
    string n;
    while (getline(cin, n)) {
        istringstream is(n);
        if (n.find('.') != string::npos) {
            double a;
            is >> a;
            auto *p = (u_int8_t *) &a;
            for (int i = 0; i < 8; ++i) {
                printf("%02x ", *p);
                p++;
            }
            printf("\n");
        } else {
            int a;
            is >> a;
            auto *p = (u_int8_t *) &a;
            for (int i = 0; i < 4; ++i) {
                printf("%02x ", *p);
                p++;
            }
            printf("\n");
        }
    }
    return 0;
}
你当前正在回复 博客/题目
存在问题!