在python中将字符串转换为8位有符号整数

在python中将字符串转换为8位有符号整数,python,ctypes,signed,unsigned-integer,Python,Ctypes,Signed,Unsigned Integer,我正在尝试使用python和ctypes修补一个电机控制系统,我需要做的一件事是获取一个文本输入,并将其转换为一个8位有符号整数 下面是我试图调用的函数的文档。应输入程序的文本为“EPOS2” 数据类型定义如下所示(请注意,“char*”等于8位有符号整数) 那么如何将“EPOS2”转换为-128到127之间的值呢 最终我要做的是这样的: import ctypes #import the module lib=ctypes.WinDLL(example.dll) #load the dl

我正在尝试使用python和ctypes修补一个电机控制系统,我需要做的一件事是获取一个文本输入,并将其转换为一个8位有符号整数

下面是我试图调用的函数的文档。应输入程序的文本为“EPOS2”

数据类型定义如下所示(请注意,“char*”等于8位有符号整数)

那么如何将“EPOS2”转换为-128到127之间的值呢

最终我要做的是这样的:

import ctypes #import the module

lib=ctypes.WinDLL(example.dll) #load the dll

VCS_OpenDevice=lib['VCS_OpenDevice'] #pull out the function

#per the parameters below, each input is expecting (as i understand it) 
#an 8-bit signed integer (or pointer to an array of 8 bit signed integers, 
#not sure how to implement that)
VCS_OpenDevice.argtypes=[ctypes.c_int8, ctypes.c_int8, ctypes.c_int8, ctypes.c_int8]

#create parameters for my inputs
DeviceName ='EPOS2'
ProtocolStackName = 'MAXON SERIAL V2'
InterfaceName = 'USB'
PortName = 'USB0'


#convert strings to signed 8-bit integers (or pointers to an array of signed 8-bit integers)
#code goes here
#code goes here
#code goes here

#print the function with my new converted input parameters


print VCS_OpenDevice(DeviceName,ProtocolStackName,InterfaceName,PortName)

您可以使用
ctypes

>>> from ctypes import cast, pointer, POINTER, c_char, c_int
>>> 
>>> def convert(c):
...     return cast(pointer(c_char(c)), POINTER(c_int)).contents.value
... 
>>> map(convert, 'test string')
[116, 101, 115, 116, 32, 115, 116, 114, 105, 110, 103]
(正如我刚刚发现的)与
ord
的输出相匹配:

>>> map(ord, 'test string')
[116, 101, 115, 116, 32, 115, 116, 114, 105, 110, 103]

尽管您的数据类型定义将其列为
char
,而不是
char*
,但我不确定您将如何处理它。

您可以使用
ctypes

>>> from ctypes import cast, pointer, POINTER, c_char, c_int
>>> 
>>> def convert(c):
...     return cast(pointer(c_char(c)), POINTER(c_int)).contents.value
... 
>>> map(convert, 'test string')
[116, 101, 115, 116, 32, 115, 116, 114, 105, 110, 103]
(正如我刚刚发现的)与
ord
的输出相匹配:

>>> map(ord, 'test string')
[116, 101, 115, 116, 32, 115, 116, 114, 105, 110, 103]

尽管您的数据类型定义将其列为
char
,而不是
char*
,但我不确定您将如何处理它。

您的接口使用的
char*
是C字符串。等效的
ctypes
类型是
c\u char\p
。使用:

import ctypes
lib = ctypes.WinDLL('example.dll')
VCS_OpenDevice = lib.VCS_OpenDevice
VCS_OpenDevice.argtypes = [ctypes.c_char_p,ctypes.c_char_p,ctypes.c_char_p,ctypes.c_char_p]

DeviceName ='EPOS2'
ProtocolStackName = 'MAXON SERIAL V2'
InterfaceName = 'USB'
PortName = 'USB0'

print VCS_OpenDevice(DeviceName,ProtocolStackName,InterfaceName,PortName)
另外,
windell
通常仅用于Windows系统DLL。如果您的接口在C头文件中声明为
\uu stdcall
windell
是正确的;否则,请使用
CDLL

此外,您的返回代码被记录为
DWORD*
,这有点奇怪。为什么不
DWORD
?如果
DWORD*
正确,要访问返回值指向的DWORD值,可以使用:

VCS_OpenDevice.restype = POINTER(c_uint32)
retval = VCS_OpenDevice(DeviceName,ProtocolStackName,InterfaceName,PortName)
print retval.contents.value

您的接口采用
char*
,它们是C字符串。等效的
ctypes
类型是
c\u char\p
。使用:

import ctypes
lib = ctypes.WinDLL('example.dll')
VCS_OpenDevice = lib.VCS_OpenDevice
VCS_OpenDevice.argtypes = [ctypes.c_char_p,ctypes.c_char_p,ctypes.c_char_p,ctypes.c_char_p]

DeviceName ='EPOS2'
ProtocolStackName = 'MAXON SERIAL V2'
InterfaceName = 'USB'
PortName = 'USB0'

print VCS_OpenDevice(DeviceName,ProtocolStackName,InterfaceName,PortName)
另外,
windell
通常仅用于Windows系统DLL。如果您的接口在C头文件中声明为
\uu stdcall
windell
是正确的;否则,请使用
CDLL

此外,您的返回代码被记录为
DWORD*
,这有点奇怪。为什么不
DWORD
?如果
DWORD*
正确,要访问返回值指向的DWORD值,可以使用:

VCS_OpenDevice.restype = POINTER(c_uint32)
retval = VCS_OpenDevice(DeviceName,ProtocolStackName,InterfaceName,PortName)
print retval.contents.value

它不是一个
char
,而是一个
char*
。从技术上讲,它是指向
char
的指针,但通常用于表示以空字节或字符串结尾的
char
数组。您如何尝试调用此函数?示例代码将让我们知道到底缺少了什么。您能否在此澄清
ctypes
的用法?您是在调用另一个库并希望使用它来实现这一点,还是可以直接使用
struct
模块模拟一个结构?我将当前得到的放在下面。希望这能让我明白我在做什么。这不是一个
char
,而是一个
char*
。从技术上讲,它是指向
char
的指针,但通常用于表示以空字节或字符串结尾的
char
数组。您如何尝试调用此函数?示例代码将让我们知道到底缺少了什么。您能否在此澄清
ctypes
的用法?您是在调用另一个库并希望使用它来实现这一点,还是可以直接使用
struct
模块模拟一个结构?我将当前得到的放在下面。希望这能让我明白我在做什么。