python - 如何在 Python 中将 DWORD 转换为字符串

标签 python c++ ctypes

我正在使用 Python 开发 C++ SDK。其中一个 API 如下所示

API

DWORD NET_DVR_GetSDKVersion(
);

文档

2 higher bytes mean the major version, 2 lower bytes mean the minor version, e.g. 0x00030000 means version 3.0.

它返回一个 DWORD 值。我做了一些研究,似乎 DWORD 在 Python 中等于 ctypes.c_ulong

但是,我从 API 得到的只是一个没有多大意义的整数 393216

代码

sdk = cdll.LoadLibrary("sdk.so")
sdk.NET_DVR_GetSDKVersion.restype = ctypes.c_long
result = sdk.NET_DVR_GetSDKVersion()

我已经使用 c_char_p 和其他几种类型设置了返回值类型,但结果并没有多大意义。

最佳答案

2 higher bytes mean the major version, 2 lower bytes mean the minor version, e.g. 0x00030000 means version 3.0.

如您在示例中所见,数字采用十六进制格式(0x 前缀)。在十六进制格式中,您可以很容易地看到字节 - 2 个十六进制数字 = 1 个字节(因为 16*16 是 256)。

您需要使用 hex(your_value_here) 将您的数字转换为十六进制并将其分为主要版本和次要版本两部分。

在你的情况下 hex(393216)返回 '0x60000' - 即 0x0作为 2 个低字节(次要版本)和 0x6作为更高的字节(主要版本),给出版本 6.0

编辑:

您还可以使用二进制操作提取您的版本。使用 your_number_here>>16将您的数字向右移动 16 位(2 字节),即删除低 2 位。

所以是 your_number>>16对于专业,your_number - (your_number>>16<<16)对于未成年人。

关于python - 如何在 Python 中将 DWORD 转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56392520/

相关文章:

c++ - 如何访问模板类型参数包中的类型索引?

c++ - 如何使用 FLTK 在 Windows、Mac OS X 和 Linux 中使窗口透明?

windows - 如何使用 python 2.7 移动 Windows 桌面图标?

python - 为什么我的 ctypes 使用 64 位 python 长尺寸为 4 而不是尺寸 8?

python - python 应用程序和注入(inject)的 DLL 之间的 IPC

python - 在 Raspberry Pi 上使用 Python smbus - 与语法混淆

python - Pandas :根据条件计算特定日期以来时间序列的百分比变化

python - 访问我的本地 flask Web 应用程序时,蝗虫请求返回错误 502

c++ - C++中递增和取消引用指针的顺序

c++ - Python CTYPE:如何释放内存?获取无效的指针错误