FIX parsing and checksum
Parsing
// Sample
char *lsFixStrBuf = "8=FIX.4.3\0019=60\00135=0\00149=ServerX\00156=ClientX\00134=1991\00152=20191126-15:14:34.016\00110=249\001";
char *lsTagBuf = "35";
char lsValBuf[1024] = { 0x00, };
if (getFixVal(lsFixStrBuf, lsTagBuf, lsValBuf) != 0x00)
{
printf("%s = %s\n", lsTagBuf, lsValBuf);
}
// get value from fix string
char* getFixVal(char *data, char *tag, char *osVal)
{
char *p, *q;
int i, j;
int maxlen = 0;
char sTmpTag[1024];
sprintf(sTmpTag, "\001%s=", tag);
if ((p = strstr(data, sTmpTag)) == 0x00)
{
return 0x00;
}
// Add this in order to prevent infinite loop
maxlen = strlen(data) - (p+strlen(tag) - data);
for(i=0, j=0 ; i < maxlen ; i++) {
if (*(p+strlen(tag)+i) == '\001')
{
osVal[j++] = 0x00;
break;
}
else if (*(p+strlen(tag)+i) {
continue;
}
osVal[j++] = *(p+strlen(tag)+i);
}
return p;
}
CheckSum
This is an example of fix checksum.
// Generate checksum value from fix string
char *GenerateCheckSum( char *buf, long bufLen )
{
static char tmpBuf[8];
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 );
}
Fix checksum is generally calculated by fix engine. You can check the function with the following code.
#include <stdio.h>
char *GenerateCheckSum( char *buf, long bufLen );
int main()
{
char *rslt;
char tmpstr[] = "8=FIX.4.2^A9=61^A35=04^A49=TARGETUAT^A56=SENDERUAT^A34=8970^A52=20111014-08:16:37";
rslt = GenerateCheckSum(tmpstr, sizeof(tmpstr));
printf("[%s]\n", rslt);
}
char *GenerateCheckSum( char *buf, long bufLen )
{
static char tmpBuf[8];
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 );
}
Leave a comment