格式化输入的小研究
来源:互联网 发布:cydia是什么软件 编辑:程序博客网 时间:2024/06/04 18:21
环境:VC6.0
typedef union
{
unsigned char pos[4];
unsigned int id;
}ID;
ID id;
文件第一行:0x01,0x02,0x03,0x04, "1st at " "a b"
问题:fscanf(fp, "%x,%x,%x,%x,", &id.pos[0], &id.pos[1], &id.pos[2], &id.pos[3]);
结果id.id = 0x04030201; 这可能是因为反了
于是:fscanf(fp, "%x,%x,%x,%x,", &id.pos[3], &id.pos[2], &id.pos[1], &id.pos[0]);
结果id.id = 0x00000004; 奇怪了
找原因: fscanf(fp, "%x,", &id.pos[3]); //执行完该语句,id.id = 0x01000000
fscanf(fp, "%x,", &id.pos[2]); //执行完该语句,id.id = 0x00020000
fscanf(fp, "%x,", &id.pos[1]); //执行完该语句,id.id = 0x00000300
fscanf(fp, "%x,", &id.pos[0]); //执行完该语句,id.id = 0x00000004
为什么呢?地址对齐?
fscanf(fp, "%x,", &id.pos[0]); //执行完该语句,id.id = 0x00000001
fscanf(fp, "%x,", &id.pos[1]); //执行完该语句,id.id = 0x00000201
fscanf(fp, "%x,", &id.pos[2]); //执行完该语句,id.id = 0x00030201
fscanf(fp, "%x,", &id.pos[3]); //执行完该语句,id.id = 0x04030201
虽然结果反了,但工作正常。
&id.pos[3]的地址>......>&id.pos[0]的地址
id.id = 0x55555555;
fscanf(fp, "%x,", &id.pos[3]); //执行完该语句,id.id = 0x01555555
fscanf(fp, "%x,", &id.pos[2]); //执行完该语句,id.id = 0x00025555
fscanf(fp, "%x,", &id.pos[1]); //执行完该语句,id.id = 0x00000355
fscanf(fp, "%x,", &id.pos[0]); //执行完该语句,id.id = 0x00000004
我想原因找到了:
memory address: 00 01 02 03 04 05 06
pos[0] pos[1] pos[2] pos[3] n n n
给pos[3]赋0x01 01 00 00 00
给pos[2]赋0x02 02 00 00 00
给pos[1]赋0x03 03 00 00 00
给pos[0]赋0x04 04 00 00 00
所以最终结果总是04。上述结果是我的推论。
修改输入0x01010101,0x02020202,0x03030303,0x04040404, "1st at " "a b"
进行调试,打开内存视图进行观察,推论是正确的。
结论就是%x返回的可能是4字节int,或其它。如何处理将%x返回成一个字节的呢?
- 格式化输入的小研究
- 关于scanf格式化输入函数的小问题
- 格式化用户输入的文本
- SQLite的小研究
- gcc中格式化输出函数的研究
- gcc中格式化输出函数的研究
- gcc中格式化输出函数的研究
- gcc中格式化输出函数的研究
- 关于XMPP格式化文本的研究
- gcc中格式化输出函数的研究
- gcc中格式化输出函数的研究
- 输入的小挫折
- java的格式化的流输入
- 字符串的格式化输入与输出
- UItextfield输入数据的格式化判断
- 解决格式化字符输入的困扰--Android
- 解决格式化字符输入的困扰--Android
- c中scanf的格式化输入函数
- C# Control.Refresh的解释是什么意思?
- 转:Infopath Repeating Table 如何控制最大行数? Limiting number of repeating tables a user can add
- 平台崩溃之operator new异常(二十一)-2010-8-9
- Strust组件—ActionServlet详解
- 嵌入式Linux下JFFS2文件系统的实现
- 格式化输入的小研究
- 图的深度优先搜索(DFS)和广度优先搜索(BFS)
- BMP格式:
- 我有微博了
- 读书笔记之编程之美 - 2.3 寻找发帖“水王”
- 程序员面试宝典总结
- var j = jQuery.noConflict(true);
- spring AOP配置的几种方法
- shell中1个方括号,2个方括号的区别