【发布时间】:2017-08-05 12:38:35
【问题描述】:
我有 C++ 代码:
unsigned char LRC_form(char* cadr)
{
unsigned char LRC=0;
unsigned int t=0;
LRC=0;
for (t=1;t<0x0D;t=t+2)
{
LRC=LRC+ASCII2hex(cadr[t])*16+ASCII2hex(cadr[t+1]);
}
return ((~(LRC))+1);
}
int main(int argc, char *argv[])
{
char *p={":010600000045"};
cout<<LRC_form(p);
}
其中 ASCII2Hex 是将字符的 ASCII 代码转换为 HEX 的函数。 我必须用 Python 写同样的东西,所以我这样做:
def LRC_FORM():
LRC=0
string_to_send = "010600000045"
Arr = list(string_to_send)
#LRC = int(Arr[1])+2
#print(LRC)
counter=0
for i in string_to_send:
numb = int(i, 16)
if counter%2 == 0:
LRC = LRC + numb * 16
else:
LRC = LRC + numb
counter = counter + 1
但是我应该如何实现 (~LRC) + 1,因为 LRC 是一个无符号字符,在我的情况下它是 int,我可以使用一些模块,如 ctypes 或 struct,但是当我这样做时:
import ctypes
import struct
cchar1=(struct.pack('1B',LRC))
cchar2= ctypes.c_char(LRC)
它没有给我我期望的结果。 LRC = 77,虽然我应该得到LRC = '77',但我得到的是b'L',所以它不会像C++中的代码那样给出相同的结果。
我怎样才能以正确的方式转换它?
提前谢谢你!
附: C++程序的输出
char *p={":010600000045"};
cout<<LRC_form(p);
给76
我正在尝试使用 Python 3 获得相同的效果
编辑 1
return LRC;
在 C 程序中给出76。
我可以在我的 Python 代码中得到相同的结果。
但是
return ((~(LRC))+1);
给出180,我不知道我应该怎么做才能在 Python 中得到同样的结果..
编辑 2
ASCII2Hex 函数:
unsigned char ASCII2hex (char ASCII)
{
if (ASCII<0x40)
return (ASCII-0x30);
else
return (ASCII-0x37);
}
enter code here
【问题讨论】:
-
我已经编辑了您的问题,因为您的代码是 C++,而不是 C。
-
你很接近:
b'L'[0] == 76。但是在 Python 中有更紧凑的方法可以做到这一点。 -
谢谢!我不明白..所以 L 应该是一个数组..?能否请您展示在 Python 中实现它的更紧凑的方法?
-
ASCII2hex 到底是做什么的?
-
b'L'是文字bytes字符串。这有点好笑,因为它看起来 像一个字符串,但是如果你遍历它,或者索引其中的单个字节,你会得到整数值。在解释器中试试这个:for i in b'ABCD': print(i)
标签: python c++ unsigned-char