Javascript中的ELD事件数据检查计算

时间:2017-12-19 17:34:02

标签: javascript binary hex

的Helloworld,

我们创建了一个ELD应用程序,我必须在Javascript中执行事件数据检查计算。

请参阅 http://eld-federal-requirements.readthedocs.io/en/latest/4-functional_requirements.html#event-data-check-calculation

我已将表3<#Table-3>中所有字符匹配。但我不确定如何计算数据检查。

我现在就这样做:

[...]

//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 << 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"

我的计算是否合适?我的客户告诉我这不好,但我不了解联邦法律计算。

感谢您的帮助。

修改

这是我的sumChars计算。也可能是错的。

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 < eventRecord.eventType.toString().length; i++) {
        if(mapping[eventRecord.eventType.toString().charAt(i)]) {
            sumChars += mapping[eventRecord.eventType.toString().charAt(i)];
        }
    }
    for (var i = 0; i < eventRecord.eventCode.toString().length; i++) {
        if(mapping[eventRecord.eventCode.toString().charAt(i)]) {
            sumChars += mapping[eventRecord.eventCode.toString().charAt(i)];
        }
    }
    for (var i = 0; i < eventRecord.date.toString().length; i++) {
        if(mapping[eventRecord.date.toString().charAt(i)]) {
            sumChars += mapping[eventRecord.date.toString().charAt(i)];
        }
    }
    for (var i = 0; i < eventRecord.time.toString().length; i++) {
        if(mapping[eventRecord.time.toString().charAt(i)]) {
            sumChars += mapping[eventRecord.time.toString().charAt(i)];
        }
    }
    for (var i = 0; i < eventRecord.vehicleMiles.toString().length; i++) {
        if(mapping[eventRecord.vehicleMiles.toString().charAt(i)]) {
            sumChars += mapping[eventRecord.vehicleMiles.toString().charAt(i)];
        }
    }
    for (var i = 0; i < eventRecord.totalEngineHours.toString().length; i++) {
        if(mapping[eventRecord.totalEngineHours.toString().charAt(i)]) {
            sumChars += mapping[eventRecord.totalEngineHours.toString().charAt(i)];
        }
    }
    for (var i = 0; i < eventRecord.latitude.toString().length; i++) {
        if(mapping[eventRecord.latitude.toString().charAt(i)]) {
            sumChars += mapping[eventRecord.latitude.toString().charAt(i)];
        }
    }
    for (var i = 0; i < eventRecord.longitude.toString().length; i++) {
        if(mapping[eventRecord.longitude.toString().charAt(i)]) {
            sumChars += mapping[eventRecord.longitude.toString().charAt(i)];
        }
    }
    for (var i = 0; i < eventRecord.cmvVin.length; i++) {
        if(mapping[eventRecord.cmvVin.charAt(i)]) {
            sumChars += mapping[eventRecord.cmvVin.charAt(i)];
        }
    }
    for (var i = 0; i < eventRecord.usernameId.length; i++) {
        if(mapping[eventRecord.usernameId.charAt(i)]) {
            sumChars += mapping[eventRecord.usernameId.charAt(i)];
        }
    }

1 个答案:

答案 0 :(得分:0)

 const dataCheck = sumChars => ((sumChars << 3) ^ 195).toString(16).substr(-2).toUpperCase();

Theres无需在此处使用字符串。二元运算符处理数字。在计算过程中额外的位无关紧要,因为<<^不受它们的影响,我们只需要通过substr(-2)来切断上面的位数...并注意你需要使用数字的十进制表示...