0

如何从语言整数到整数有很多线程,但反之亦然。我正在使用本机 c 库opengl,移植到 pythonpyopengl中。

令人讨厌的是,所有函数都需要 c 变量数据类型。我的问题很简单,如何将函数 ac 语言类型整数作为该函数的参数。我已经ctypes使用以下代码尝试了该库:

import ctypes
    
integer = 0
cinteger = ctypes.c_int(int)

print(cinteger)
output: c_long(0)

问题是由于某种原因,所有函数都返回不接受c_int的 64 位长。pyopengl

对于这个特定的用例,我似乎找不到关于ctypes库的好教程。除非必要,否则我宁愿避免使用类对象,因为我只需要一个 c 整数值。

我也尝试过 pythonbin()方法来尝试复制 ac 整数的 32 位二进制等价物,但我无法理解它。

编辑:仅针对某些上下文,这是我要调用的函数:

shader = """                 #just some code i need for the library to work
insert c code here
int texture
"""
pointer = glgetattriblocation("texture",shader) #returns a ctype integer
glUnifrorm1i(pointer,integer value)  #here is my real problem, because for 
# this integer value i cant just do glgetattriblocation which would give me   
# an error and also because it is a pointer to my data and the int is the
# data that i want to pass, therefore i need a 4 byte int following the same 
# convention as the c language for this function to work.
4

1 回答 1

-1

我建议使用 python struct library

from struct import *
struct('i', 66)

b'B\x00\x00\x00'

https://docs.python.org/3/library/struct.html#format-strings 阅读如何格式化数据类型。

于 2020-11-01T22:02:00.970 回答