- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
当我使用 ctypes 读取二进制数据时,它无法正常工作。
二进制数据
03 00 00 00 49 7B 00 00 00 00 00 00
Python 代码
from ctypes import *
class DataStructure(Structure):
_fields_ = [
("long1", c_ulong),
#("long2", c_ulong),
("longlong", c_ulonglong)
]
binaryfile = "./ULongLong"
f = open(binaryfile, "rb")
mystruct = DataStructure()
f.readinto(mystruct)
if __name__ == "__main__":
print mystruct.long1
#print mystruct.long2
print mystruct.longlong
结果
3
0
但是当我读取二进制数据并取消注释 python 代码时,它工作得很好。
03 00 00 00 03 00 00 00 49 7B 00 00 00 00 00 00
结果
3
3
31561
这似乎是一个错误。有人可以帮我解决这个问题吗?任何建议将不胜感激。
环境:Windows 7 x64Python 2.7 x32ctypes 1.1.0
最佳答案
据我了解,您遇到了 structure packing 的问题。看起来您的代码正在读取“03 00 00 00 49 7B 00 00”(字大小 - 64 位),但仅使用前 4 个字节“03 00 00 00”。
更新:根据 eryksun ,上面的分析是正确的。只需在 DataStructure 的定义中设置 _pack_ = 1
即可。
一些 C 代码的实验:
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
struct data_structure {
uint32_t long1;
uint64_t longlong;
};
int main(int argc, char const *argv[])
{
FILE *fd, *other_fd;
struct data_structure my_struct;
my_struct.long1 = 3;
my_struct.longlong = 31561;
fd = fopen("ULongLong", "wb");
if (!fd)
{
printf("Unable to open file!");
return 1;
}
fwrite(&my_struct, sizeof(struct data_structure), 1, fd);
fclose(fd);
exit(0);
}
编译运行后,检查ULongLong
文件:
$ hexdump ULongLong
00000000 0300 0000 0000 0000 497b 0000 0000 0000
00000010
有时你可能会在第 5 到第 8 字节中得到一些垃圾:
$ hexdump ULongLong
00000000 0300 0000 ff7f 0000 497b 0000 0000 0000
00000010
$ hexdump ULongLong
0000000 0003 0000 7fff 0000 7b49 0000 0000 0000
0000010
这个二进制文件正确吗?
关于Python ctypes 与 io.readinto 不能很好地工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24185502/
下类时合上笔记本电脑。当我回到家并打开它时,它已重新启动。现在,当我打开 Titanium Developer 时,它会立即崩溃。 所以我现在也打不开。关于如何调试或找出问题所在的任何想法? 甚至尝试
我们想共享运行时项目二进制文件。所以每个团队成员都可以使用当前的工作版本。 在 SVN 中存储运行时二进制文件是否可以接受/很好? 最佳答案 不,不要将二进制文件存储在其源代码旁边(除非您有充分的理由
我在 PHP 中使用循环来显示 CMS 管理部分中的用户数据。每行(用户)都包含一些我想要连接到命令的图标(例如:编辑、删除等)。表的最后一行有空的输入字段,带有单个图标(命令),以允许添加新用户。这
如果这是一个新手问题,请原谅我,我昨天开始学习 Django,并且我尽量不要养成坏习惯,即我试图从一开始就以“django 方式”做事。 我有一个 View 接收二进制数据作为 http post 字
使用 swift 2.1 我正在寻找一种将非可选类型数组分配给可选类型数组的好方法,其中类型相同。这是我尝试过的一些方法: var foos: [Int?] = [] let bars: [Int]
James Gosling,加拿大计算机科学家,完成了 Java 的原始设计,并实现了 Java 最初版本的编译器和虚拟机,也是公认的 “Java 之父”。 Evrone 是一家企业软件开发公司,
我在两个项目上使用 Twitter Bootstrap,一个是静态 HTML 网站,另一个是 Rails 应用程序。当我在桌面浏览器上测试网站时,调整大小有效。但是当我在手机 [Nokia E72]
我可以使用全日历。但我有一个侧边栏,用户可以折叠它,然后内容框会变大,但是当用户这样做时,日历就不那么好了。 所以我正在考虑窗口调整大小功能,但这仅在浏览器窗口更改时才有效,那么当容器变大或变小时如何
我正在尝试使用 C# 和 LINQ 在数据库中查询每日活跃用户。我有一个运行良好的 SQL 查询。它在 u.UserId).Distinct().Count() } 测试后,当我必须选择 (
我目前正在开发部署目标为 7.1 的 iOS 应用程序。我的大部分测试都是在 iOS8 环境中执行的,没有任何问题。我连接了一个 iOS7 (5s) 测试设备,发现通过手机显示的 View 是 3.5
我有这样的代码: Floating left. Floating right. BlahBlah Container 允许我将页脚推到页面底部,但如果我想让左右栏跨越接触页脚的高度
从 PHP4 和 Cake 1.3 开始,我一直在使用 debug($data);在 CakePHP 中调试诸如模型输出之类的东西。 但是,自从升级到 PHP5.4 后,我注意到 debug($dat
我在Canvas上画我的游戏,一切都是上帝,但我把它改成了JPanel,但现在它不能正常工作,这是代码,你可以复制它们,你就会看到问题出在哪里(我有一个菜单,单击按钮后它应该创建新线程,我想在那里画画
我尝试用 scrapy 抓取一页。我用 FireXpath(一个 firefox 插件)找到了 xpath,它看起来不错。但是对于 Scrapy,我没有得到任何结果。 我的 python 程序如下所示
我想在页面加载时加载 fancybox。它工作正常,但我对它的高度有疑问。假设页面的高度为 3000px,而 fancybox 的高度为 1500px。如果你想看到页面的最低部分,都应该向下滚动。不幸
IE 大小调整问题!?代码非常简单:我有一个 div,我想要一个占 DIV 100% 的文本框。它必须显示 div 的红线(如果我使用 height:100%, width:100% 它会侵 ecli
我有一组看起来像这样的代码: if(self.property == A) { [self.infoManager getThingA:^(NSString *thing, NSError *
我认为 git clone 使用 STDERR。 我现在想将它重定向到 STDOUT 以使用此 hack . 我遇到了一些问题(另外,我使用很棒的 stderred 库自动将 STDERR 着色为红色
我的问题是我的表单正在提交(到节点/express api),如果我console.log req.body(@ api),结果是一个空对象。但是,如果我在客户端上 console.log ,则序列化
我对 addon-sdk 还很陌生,并且遇到了一个对我来说无法解释的问题。到目前为止,我一直在使用 jpm run 来测试一切 - 并且一切都很好。现在,我即将完成我想要完成的任务,所以我想在“普通”
我是一名优秀的程序员,十分优秀!