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

问题描述 投票:0回答:1

你好,世界,

我们创建一个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)];
        }
    }
javascript binary hex
1个回答
0
投票
 const dataCheck = sumChars => ((sumChars << 3) ^ 195).toString(16).substr(-2).toUpperCase();

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

© www.soinside.com 2019 - 2024. All rights reserved.