cs:app学习笔记(1):show-bytes
2017-10-26 01:11
441 查看
showbytes
代码总览
代码说明
编译与结果显示
将
在cs:app中,提供的代码格式是如下:
虽然对于程序不影响,但是我更喜欢我的写法,这样更像指向无符号字符型的指针。
这三个函数,将不同类型的对象的指针都强制转化成
主函数与测试函数。
在linux 64位机的输出结果
解释:
123456的16进制表示为 0x00003039,由于linux 64位采用小端存储,所以先输出低位39,然后再输出30。所以结果如上述表示。
代码总览
代码说明
编译与结果显示
show—bytes
代码总览
#include <stdio.h> typedef unsigned char* byte_pointer; void show_bytes(byte_pointer start, int len){ int 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) val; int *pval = &ival; show_int(ival); show_float(fval); show_pointer(pval); } int main(int argc, char const *argv[]) { int test = 12345; test_show_bytes(test); }
代码说明
typedef unsigned char* byte_pointer;
将
unsigned char*命名为
byte_pointer。
在cs:app中,提供的代码格式是如下:
typedef unsigned char *byte_pointer;
虽然对于程序不影响,但是我更喜欢我的写法,这样更像指向无符号字符型的指针。
void show_bytes(byte_pointer start, int len){ int i; for (i = 0; i < len; i++) printf ("%.2x", start[i]); printf("\n"); }
show-bytes这个函数将传入无符号字符对象的指针,和该对象的字节数,然后通过在
for循环中,使用
printf ("%.2x", start[i]);进行格式化输出。
%.2x确保整数都以两位16进制数输出。
start代表对象的首地址,
start[i]代表从
start[0]开始第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_pointer(void *x){ show_bytes((byte_pointer)&x, sizeof (void *)); }
这三个函数,将不同类型的对象的指针都强制转化成
unsigned char*类型,并用
sizeof关键字表示该数据所用字节数。
void test_show_bytes(int val){ int ival = val; float fval = (float) val; int *pval = &ival; show_int(ival); show_float(fval); show_pointer(pval); } int main(int argc, char const *argv[]) { int test = 12345; test_show_bytes(test); }
主函数与测试函数。
编译与结果显示
编译命令gcc show-bytes.c -o main ./main
在linux 64位机的输出结果
39300000 00e44046 987de0a9fe7f0000
解释:
123456的16进制表示为 0x00003039,由于linux 64位采用小端存储,所以先输出低位39,然后再输出30。所以结果如上述表示。
相关文章推荐
- cs:app 学习笔记(3)metore
- 2011斯坦福大学iOS应用开发教程学习笔记(第二课)My First iOS App
- iOS学习笔记01-APP相关
- FX3学习笔记2-cs-make[1]: [post-build] Error 2 (ignored)
- iOS9-by-Tutorials-学习笔记二:App-Search
- iOS学习笔记:app不满屏
- JQuery学习笔记之JQuery效果hide、show以及toggle方法的callback参数
- 学习笔记 AppCompatEditText
- iOS学习笔记4-xcode构建iOS app时为什么分version和build两个版本号
- 2011斯坦福大学iOS应用开发教程学习笔记(第二课)My First iOS App
- 学习WP7应用开发的笔记--在App和Page中使用资源集合的注意点1
- PHP 开发 APP 接口 学习笔记与总结 - APP 接口实例 [5] 版本设计分析及数据表设计
- React学习笔记_从create-react-app学习webpack
- Android开发学习笔记(六):最简单的办法实现APP欢迎界面
- flash cs 3组件学习笔记
- 学习笔记:App-V测试错误代码4505CD-1690150A-20000194
- 《CS:APP》 chapter 3 Machine-Level Representation of Programs 笔记
- iOS9-by-Tutorials-学习笔记三:Your-App-on-the-Web
- Android - building your frist app 学习笔记(二)
- App内购之 Ray Wenderlich 的 IAP 文章的学习笔记