Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将python 2移植到python 3串行文件_Python_Encode_Porting - Fatal编程技术网

将python 2移植到python 3串行文件

将python 2移植到python 3串行文件,python,encode,porting,Python,Encode,Porting,我在将一个旧的python2脚本移植到python3时遇到了一个问题,而python3过去工作得很好。以下是Python 2代码,其目的是创建一个命令,通过串行方式发送: def makeCommand(command, data=''): length = len(command) + len(data) + 1 buf = [chr(length), chr(0xFF), command, data] checksum = 256 - sum[(ord(x) for

我在将一个旧的python2脚本移植到python3时遇到了一个问题,而python3过去工作得很好。以下是Python 2代码,其目的是创建一个命令,通过串行方式发送:

def makeCommand(command, data=''):
    length = len(command) + len(data) + 1
    buf = [chr(length), chr(0xFF), command, data]
    checksum = 256 - sum[(ord(x) for x in buf)]%256
    buf.append(chr(checksum))

    return ''.join(buf)


def main():
    makeCommand('A')

main() 
Python3,有一些更新:

def makeCommand(command, data=''):
    length = len(command) + len(data) + 1
    buf = [chr(length), chr(0xFF), command, data]
    checksum = 256 - sum[x for x in buf)]%256
    buf.append(chr(checksum))

    return ''.join(buf).encode()
现在,当我运行它们时,我得到:

  • Python 2:\x02\xFFA\CRC
  • Python 3:\x02\xc3\xbfA\CRC
Python2的输出正是我所期望的,但我不理解Python3的输出。为什么我的字节被拆分为2(\xc3\xbf而不是\xFF),CRC也有同样的问题

如何使用Python3获得相同的输出

多谢各位