2015-09-22 14 views
5

Ho un FixMessage e voglio calcolare CheckSum manualmente.Come calcolare manualmente CheckSum in Fix?

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

Il bodylength qui si calcola:

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) 

Il checksum è 157 (10 = 157). Come si calcola in questo caso?

risposta

6

È necessario sommare ogni byte del messaggio fino a escludere il campo checksum. Quindi prendi questo numero modulo 256 e stampalo come un numero di 3 caratteri con zero iniziali (ad es. Checksum = 13 diventerebbe 013).

collegamento dal wiki FIX: FIX checksum

Un esempio di implementazione in C: example

+0

"sum ogni byte nel messaggio ". Puoi scriverlo in questo caso? – anhtv13

+0

@ANguyen modificato con link ed esempio –

+0

Ancora non capisco quell'esempio. Scrivo C#. Se ho un FixMessage: {8 = FIX.4.29 = 4935 = 534 = 149 = ARCA52 = 20150916-04: 14: 05.30656 = TW10 = 157}. Come contarlo a 157? – anhtv13

1
static void Main(string[] args) 
    { 
     //10=157 
     string s = "8=FIX.4.2|9=49|35=5|34=1|49=ARCA|52=20150916-04:14:05.306|56=TW|"; 
     byte[] bs = GetBytes(s); 
     int sum=0; 
     foreach (byte b in bs) 
      sum = sum + b; 
     int checksum = sum % 256; 
    } 
    //string to byte[] 
    static byte[] GetBytes(string str) 
    { 
     byte[] bytes = new byte[str.Length * sizeof(char)]; 
     System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length); 
     return bytes; 
    } 
+0

Nota 1: il separatore nei messaggi FIX è il carattere SOH; non il | simbolo (simbolo del tubo). Il valore byte del carattere SOH è 1. Invece di "" 8 = FIX.4.2 | ... "' scrivi '" 8 = FIX.4.2 \ u0001 ... "' –

+2

Nota 2: Per ottenere un byte corretto- array di una stringa, fare riferimento a [FieldBase.metodo getTotal nella libreria QuickFIX/N] (https://github.com/connamara/quickfixn/blob/master/QuickFIXn/Fields/FieldBase.cs). Fondamentalmente la riga 'byte [] bs = GetBytes (s);' dovrebbe diventare 'byte [] bs = System.Text.Encoding.UTF8.GetBytes (s);' –

+0

Ok, grazie. Capisco. – anhtv13

1

Ready-to-run esempio C adattato da here

8 = FIX .4.2 | 9 = 49 | 35 = 5 | 34 = 1 | 49 = ARCA | 52 = 20.150.916-04: 14: 05,306 | 56 = TW | 10 = 157 |

#include <stdio.h> 

void GenerateCheckSum(char *buf, long bufLen) 
{ 
     unsigned sum = 0; 
     long i; 
     for(i = 0L; i < bufLen; i++) 
     { 
      unsigned val = (unsigned)buf[i]; 
      sum += val; 
      printf("Char: %02c Val: %3u\n", buf[i], val); // print value of each byte 
     } 
     printf("CheckSum = %03d\n", (unsigned)(sum % 256)); // print result 
} 

int main() 
{ 
    char msg[] = "8=FIX.4.2\0019=49\00135=5\00134=1\00149=ARCA\00152=20150916-04:14:05.306\00156=TW\001"; 
    int len = sizeof(msg)/sizeof(msg[0]); 
    GenerateCheckSum(msg, len); 
} 

punti da notare

  • GenerateCheckSum prende l'intero messaggio FIX eccezione campo checksum
  • Delimiter SOH è scritto come \001 che ha ASCII value1