Warm tip: This article is reproduced from serverfault.com, please click

quickfix-如何手动计算 FIX 中的 CheckSum?

(quickfix - How to calculate CheckSum in FIX manually?)

发布于 2015-09-22 03:53:58

我有一个FixMessage,我想手动计算校验和。

8=FIX.4.2|9=49|35=5|34=1|49=ARCA|52=20150916-04:14:05.306|56=TW|10=157|

此处计算体长:

8=FIX.4.2|9=49|35=5|34=1|49=ARCA|52=20150916-04:14:05.306|56=TW|10=157|
0        + 0  + 5  + 5  + 8     + 26                     + 5   + 0  = 49(correct)

校验和为 157 (10=157)。在这种情况下如何计算?

Questioner
anhtv13
Viewed
0
TT. 2020-03-18 02:08:57

你需要将消息中的每个字节相加,但不包括校验和字段。然后将这个数字取模 256,并将其打印为 3 个字符的数字,并带有前导零(例如,校验和 = 13 将变为 013)。

来自 FIX wiki 的链接:FIX 校验和

C 中的示例实现,取自onixs.biz

char *GenerateCheckSum( char *buf, long bufLen )
{
    static char tmpBuf[ 4 ];
    long idx;
    unsigned int cks;

    for( idx = 0L, cks = 0; idx < bufLen; cks += (unsigned int)buf[ idx++ ] );
    sprintf( tmpBuf, "%03d", (unsigned int)( cks % 256 ) );
    return( tmpBuf );   
}