如何在python字符串中表示十六进制值?

usu*_*arr 3 c python ctype

我在C中有一些代码需要char缓冲区,如下所示:

 char buf[ 64 ];
    buf[0] = 0x01;
    buf[1] = 0x11;
    buf[2] = 0x61;
    buf[3] = 0x08;
    buf[4] = 0x01;
Run Code Online (Sandbox Code Playgroud)

我试图在Python中创建此缓冲区,并将指针传递给C代码.我创建了如下buf,

buf = create_string_buffer('0x01 0x11 0x61 0x08 0x01', 64)
Run Code Online (Sandbox Code Playgroud)

但是当我将指针传递给我的C代码时,看起来C代码正在读取01的ASCII代码,而不是十六进制文字.

我该如何解决?

Sim*_*ons 10

在Python中,十六进制文字被转义 \x

所以在你的情况下,传递给你的c函数的字符串将被写为

'\x01\x11\x61\x08\x01'
Run Code Online (Sandbox Code Playgroud)

有一个表在这里的python文档列出了蟒蛇理解转义序列.