-2

I am running into problems with Hex values and python. I am trying to write a function which performs a bytewise XOR and returns a Hex value.

Basically I am trying to convert this C# code to Python:

private byte[] AddParity(string _in)
{
byte parity = 0x7f;
List<byte> _out = new List<byte>();
ASCIIEncoding asc = new ASCIIEncoding();

byte[] bytes = asc.GetBytes(_in + '\r');
foreach (byte bt in bytes)
    {
    parity ^= bt;
    _out.Add(bt);
    }
_out.Add(parity);
return _out.ToArray();
}

Can someone point me in the right direction?

Ronald
  • 17
  • 1
  • 6
  • 3
    SO is not a code translation service. What kind of problems are you running into? – Nathan Apr 28 '14 at 15:48
  • 2
    Can you show your python code so far? – tnw Apr 28 '14 at 15:48
  • The problem I am running into is the conversion from string to HEX and the XOR. XOR expects an int. The only problem I have is the conversion of the string and HEX value for the XOR. The rest is piece of cake. The function loops through every character of the string and performs a XOR on that character and the parity var (\x7F). The parity variable is updated with the result of the XOR. Once the whole loop is done, the parity variable is added to the string, which has to be hexadecimal. – Ronald Apr 28 '14 at 16:25

1 Answers1

1
parity = 0x7f
parities = [int(item,16) ^ parity for item in "4e 7f 2b".split()]
#or maybe
parities = [ord(item) ^ parity for item in "somestring"]

I guess you are using this as some sort of checksum

parity = 0x7f
bits = []
for bit in "somestring":
    parity ^= ord(bit)
    parity &= 0xFF #ensure width
    bits.append(bit)
bits.append(parity)

to do the checksum more pythonically you could do


this is the answer you want

bytestring = "vTest\r"
bits = chr(0x7f) + bytestring
checksum = reduce(lambda x,y:chr((ord(x)^ord(y))&0xff),bits)
message = bytestring+checksum
print map(lambda x:hex(ord(x)),message)
#Result:['0x76', '0x54', '0x65', '0x73', '0x74', '0xd', '0x32']
# ser.write(message)

if you want to see the hex values

print map(hex,parities)

or to see the binary

print map(bin,parities)
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
  • Ah much closer. Thanks! If I'm not mistaken, this outputs an array? (I'm kind of new to Python, still learning) – Ronald Apr 28 '14 at 17:29
  • see my edit ... I got a little trigger happy ... and didnt fully understand what your code was doing ... I think my edit fully answers your question – Joran Beasley Apr 28 '14 at 18:02
  • Ah way closer. Thanks. I'll try to explain: I want to send IBIS data to a Display. It connects via RS232. Say I want the word "Test" on the display, I have to send the following hexadecimal data: "\x76\x54\x65\x73\x74\r\x32" --> vTest \r and the parity is \x32. So the function should get a string and return the above string. Convert the string to HEX and then add the valid parity. Using your solution I seem to get the wrong parity value. – Ronald Apr 28 '14 at 18:12
  • This is the same code in VB6. txt = string & vbCr P = &H7F For i = 1 To Len(txt) P = P Xor Asc(Mid$(txt, i, 1)) Next i txt = txt & Chr$(P) – Ronald Apr 28 '14 at 18:17
  • @Ronald why is the x76 at the start of the message? that is a 'v' making the message `'vTest\r2'` ... which is the exact string you get back if you run the code against `bytestring="vTest\r"` – Joran Beasley Apr 28 '14 at 18:53
  • The 'v' is a command for the LED display to indicate a destination is coming. – Ronald Apr 28 '14 at 19:37
  • With some adjusting I got it to work the way I want. Thanks! Learned a lot! – Ronald Apr 29 '14 at 14:40