Pankaj Singh
Pankaj Singh

Reputation: 586

How to calculate the CheckSum of a FIX message in python

8=FIX.4.4|9=102|35=D|34=1|49=XXX|52=20200206-21:15:13.000|56=YYY|11=321|41=123|54=B|55=LNUX|58=This is a new message.|10=179|

This above is my FIX message. The message tells me a checksum of 179.

How do I calculate this manually (for verification purposes)

Upvotes: 1

Views: 1464

Answers (2)

larboyer
larboyer

Reputation: 159

I will just add two small refinements. For one, 'tailPosition' is not specified in this code, but it should always be -7 since '10=xyz|' is always 7 characters.

Second, while each FIX field is terminated with the ascii SOH char (which is unprintable), it is commonly written and frequently worked with using a pipe, so I added a test to be sure pipes are counted as if they were the SOH char (ascii 1).

# Strip off checksum at end of msg: "10=xyz|"
tailPosition = -7
msgForCheckSum = raw_message[:tailPosition]
sum = 0
for c in msgForCheckSum:
    # If written as pipe, only add SOH (ascii 1)
    if c == "|":
        sum += 1
    else:
        sum += ord(c)
sum = sum % 256
print(sum)

Upvotes: 0

Pankaj Singh
Pankaj Singh

Reputation: 586

I figured it out. Remove the tail tags and then:

    msgForCheckSum = raw_message[:tailPosition]
    sum = 0
    for c in msgForCheckSum:
        sum += ord(c)
    sum = sum % 256
    return sum

Tail starts at 'chr(1)' + '10='

Upvotes: 1

Related Questions