我试图将这段c代码转换成python,但是我在char*到{}的转换中遇到问题:void sendAsciiCommand(string command) {
unsigned int nchars = command.length() + 1; // Char count of command string
unsigned int nshorts = ceil(nchars / 2); // Number of shorts to store the string
std::vector regs(nshorts); // Vector of short registers
// Transform char array to short array with endianness conversion
unsigned short *ascii_short_ptr = (unsigned short *)(command.c_str());
for (unsigned int i = 0; i < nshorts; i++)
regs[i] = htons(ascii_short_ptr[i]);
return std::string((char *)regs.data());
}
我在Python2.7中尝试过以下代码:
^{pr2}$
但它给了我一个错误:ValueError: string length not a multiple of item size
有什么帮助吗?在