Python 3.x 在python3中创建十六进制校验和时出现问题
好的,我正在控制一台工业打印机 我需要发送一个十六进制UDP数据包 这是这个包背后的理论 CHKSUM:256模(LEN+ST#+CMD+DATA)之和 我的最后一个包需要看起来像这样 02 00 06 00 46 07 00 005303Python 3.x 在python3中创建十六进制校验和时出现问题,python-3.x,hex,mod,Python 3.x,Hex,Mod,好的,我正在控制一台工业打印机 我需要发送一个十六进制UDP数据包 这是这个包背后的理论 CHKSUM:256模(LEN+ST#+CMD+DATA)之和 我的最后一个包需要看起来像这样 02 00 06 00 46 07 00 005303 def startprint(msgID): STX = "02" LEN = "0006" ST = "00" CMD = "46" D
def startprint(msgID):
STX = "02"
LEN = "0006"
ST = "00"
CMD = "46"
DATA = msgID
ETX = "03"
CHKSUM = hex(int(LEN,16)+ int(CMD,16) + int(DATA))
print(CHKSUM[2:])
testmod = int(CHKSUM[2:])%256
test1 = str(testmod) #This Is Correct
print(test1) #This is Corret my checksum should be 53
packet = bytearray();
packet.append(int(STX))
packet.append(int(LEN))
packet.append(int(ST))
packet.append(int(CMD))
packet.append(int(DATA))
packet.append(int(testmod)) #This converts my hex to 5 for some reason, and not 53
print(packet)
startprint("07")
这就是我得到的
字节数组(b'\x02\x06\x00.\x075')
不知道我哪里出了问题,我的头撞在墙上好几个小时了,四处摸索,毫无进展
我哪里做错了
def startprint(msgID):
STX = "02"
LEN = "0006"
ST = "00"
CMD = "46"
DATA = msgID
ETX = "03"
CHKSUM = hex(int(LEN,16)+ int(CMD,16) + int(DATA))
print(CHKSUM[2:])
testmod = int(CHKSUM[2:])%256
test1 = str(testmod) #This Is Correct
print(test1) #This is Corret my checksum should be 53
packet = bytearray();
packet.append(int(STX))
packet.append(int(LEN))
packet.append(int(ST))
packet.append(int(CMD))
packet.append(int(DATA))
packet.append(int(testmod)) #This converts my hex to 5 for some reason, and not 53
print(packet)
startprint("07")