From ca2d978370040de111942fb1bb5a2a2eab4731e2 Mon Sep 17 00:00:00 2001 From: will_zorin <> Date: Thu, 7 Nov 2024 22:36:57 +0800 Subject: [PATCH] the first commit: add show_bytes.c --- README | 1 + show_bytes.c | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 README create mode 100644 show_bytes.c diff --git a/README b/README new file mode 100644 index 0000000..37c5b78 --- /dev/null +++ b/README @@ -0,0 +1 @@ +This project just do for my csapp practice, over. diff --git a/show_bytes.c b/show_bytes.c new file mode 100644 index 0000000..75a952a --- /dev/null +++ b/show_bytes.c @@ -0,0 +1,36 @@ +#include + +typedef unsigned char *byte_pointer; + +void show_bytes(byte_pointer start, size_t len) { + size_t i; + for (i = 0; i < len; i++) + printf(" %.2x", 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; + int *pval = &ival; + show_int(ival); + show_float(fval); + show_pointer(pval); +} + +void main(void) { + int test_val = 12345; + test_show_bytes(test_val); +}