title: ELD Event Data Check Calculation in Javascript
body: <p>Helloworld,</p>
  
  <p>We create an ELD app and I must perform Event Data Check Calculation in Javascript.</p>
  
  <p>See
  <a href="http://eld-federal-requirements.readthedocs.io/en/latest/4-functional_requirements.html#event-data-check-calculation" rel="nofollow noreferrer">http://eld-federal-requirements.readthedocs.io/en/latest/4-functional_requirements.html#event-data-check-calculation</a> </p>
  
  <p>I already sum all characters match in table 3 &lt;#Table-3>. But I'm not sure how to calculate the data-check.</p>
  
  <p>I make this for now:</p>
  
  <pre><code>[...]
  
  //4.4.5.1.2 Event Data Check Calculation
      console.log(sumChars); // "722"
      var binary = sumChars.toString(2);
      //Output 8-bit byte, after operation is done.
      binary = ("000000000" + binary.toString(2)).substr(-8);
  
      //1. Three consecutive circular shift left (rotate no carry -left) operations; and
      binary = binary &lt;&lt; 3;
      //Output 8-bit byte, after operation is done.
      binary = ("000000000" + binary.toString(2)).substr(-8);
  
      //2. A bitwise exclusive OR (XOR) operation with the hexadecimal value C3 (decimal 195; binary 11000011).
      binary = binary ^ 11000011;
      //Output 8-bit byte, after operation is done.
      binary = ("000000000" + binary.toString(2)).substr(-8);
  
      //The event data check value must be the hexadecimal representation of the output 8-bit byte.
      console.log(parseInt(binary, 2).toString(16).toUpperCase()); // "1B"
  </code></pre>
  
  <p>Did my calculation is good or not? My client tell me this is not good, but I don't understand the federal law calculation.</p>
  
  <p>Thanks for your help.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Here is my sumChars calculation. Possibly wrong too.</p>
  
  <pre><code>var mapping = {"1":1,"2":2,"3":3,"4":4,"5":5,"6":6,"7":7,"8":8,"9":9,
      "A":17,"B":18,"C":19,"D":20,"E":21,"F":22,"G":23,"H":24,"I":25,"J":26,"K":27,"L":28,"M":29,"N":30,"O":31,"P":32,"Q":33,"R":34,"S":35,"T":36,"U":37,"V":38,"W":39,"X":40,"Y":41,"Z":42,
      "a":49,"b":50,"c":51,"d":52,"e":53,"f":54,"g":55,"h":56,"i":57,"j":58,"k":59,"l":60,"m":61,"n":62,"o":63,"p":64,"q":65,"r":66,"s":67,"t":68,"u":69,"v":70,"w":71,"x":72,"y":73,"z":74};
      //For all attributes, sum all caracters of all attributes with mapping table.
      var sumChars = 0;
      for (var i = 0; i &lt; eventRecord.eventType.toString().length; i++) {
          if(mapping[eventRecord.eventType.toString().charAt(i)]) {
              sumChars += mapping[eventRecord.eventType.toString().charAt(i)];
          }
      }
      for (var i = 0; i &lt; eventRecord.eventCode.toString().length; i++) {
          if(mapping[eventRecord.eventCode.toString().charAt(i)]) {
              sumChars += mapping[eventRecord.eventCode.toString().charAt(i)];
          }
      }
      for (var i = 0; i &lt; eventRecord.date.toString().length; i++) {
          if(mapping[eventRecord.date.toString().charAt(i)]) {
              sumChars += mapping[eventRecord.date.toString().charAt(i)];
          }
      }
      for (var i = 0; i &lt; eventRecord.time.toString().length; i++) {
          if(mapping[eventRecord.time.toString().charAt(i)]) {
              sumChars += mapping[eventRecord.time.toString().charAt(i)];
          }
      }
      for (var i = 0; i &lt; eventRecord.vehicleMiles.toString().length; i++) {
          if(mapping[eventRecord.vehicleMiles.toString().charAt(i)]) {
              sumChars += mapping[eventRecord.vehicleMiles.toString().charAt(i)];
          }
      }
      for (var i = 0; i &lt; eventRecord.totalEngineHours.toString().length; i++) {
          if(mapping[eventRecord.totalEngineHours.toString().charAt(i)]) {
              sumChars += mapping[eventRecord.totalEngineHours.toString().charAt(i)];
          }
      }
      for (var i = 0; i &lt; eventRecord.latitude.toString().length; i++) {
          if(mapping[eventRecord.latitude.toString().charAt(i)]) {
              sumChars += mapping[eventRecord.latitude.toString().charAt(i)];
          }
      }
      for (var i = 0; i &lt; eventRecord.longitude.toString().length; i++) {
          if(mapping[eventRecord.longitude.toString().charAt(i)]) {
              sumChars += mapping[eventRecord.longitude.toString().charAt(i)];
          }
      }
      for (var i = 0; i &lt; eventRecord.cmvVin.length; i++) {
          if(mapping[eventRecord.cmvVin.charAt(i)]) {
              sumChars += mapping[eventRecord.cmvVin.charAt(i)];
          }
      }
      for (var i = 0; i &lt; eventRecord.usernameId.length; i++) {
          if(mapping[eventRecord.usernameId.charAt(i)]) {
              sumChars += mapping[eventRecord.usernameId.charAt(i)];
          }
      }
  </code></pre>