python - python中int数组中的unsigned char数组

标签 python c int type-conversion wiringpi

我尝试使用 wiringPi's lcdCharDef() 在树莓派上的 2004 液晶显示器上定义一个新字符(大写德语变音符号“Ä”)

这是我的代码

import wiringpi2 as wiringpi

# Ä
cap_umlaut_a = [
    int('0b01010', 2),
    int('0b00100', 2),
    int('0b01010', 2),
    int('0b10001', 2),
    int('0b11111', 2),
    int('0b10001', 2),
    int('0b10001', 2),
    int('0b00000', 2)
]

print(cap_umlaut_a) # [10, 4, 10, 17, 31, 17, 17, 0]

wiringpi.lcdCharDef(lcd_handle, 0, cap_umlaut_a)

当我运行这段代码时,出现以下错误:

TypeError: in method 'lcdCharDef', argument 3 of type 'unsigned char [8]'

我希望这些 intsunsigned chars 相同

[编辑]
在代码的不同部分,我使用 ord(char) 仅将一个字符转换为 unsigned int。这能得出正确的答案吗?

如何将数组转换/转换为可以接受的类型?

附言(请注意(据我所知)python wiringPi 库只是包装了 wiringPi 的 C 函数)

[编辑]
我在 github 上开了一个问题:https://github.com/WiringPi/WiringPi2-Python/issues/20

最佳答案

我做了一些研究,找到了相关 python 绑定(bind)的来源 at this github repo .

有问题的行是

res3 = SWIG_ConvertPtr(obj2, &argp3,SWIGTYPE_p_unsigned_char, 0 | 0 );

如您所见,您必须传入相当于指向 unsigned char 的指针的 python。根据this thread ,相当于一个字节串。这意味着正确的调用是

import struct
wiringpi.lcdCharDef(lcd_handle, 0, struct.pack('8B', *cap_umlaut_a))

应该等同于

wiringpi.lcdCharDef(lcd_handle, 0, b'\x0A\x04\x0A\x11\x1F\x11\x11\x00')

关于python - python中int数组中的unsigned char数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31711978/

相关文章:

Python:语法错误:关键字不能是表达式

c - 结构体中的结构体没有成员

python - 如何仅将嵌套列表的某些字符串转换为整数?

python - 对具有随机异常的字典列表进行排序(不排序)

python - 如何使用 Plotly Express 显示 y 轴上的每个值?

python - Tornado ,在回调函数中访问附加数据?

C - 为什么我不断打印内存地址而不是数组中的值

c - 我应该在哪里调用 Free() 函数?

c++ - 将int转换为int值的char

Java,简化检查 int 数组是否包含 int