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