地狱世界,
我们创建了一个ELD应用程序,我必须在Javascript中执行事件数据检查计算。
请参阅requirements.html#event-data-check-calculation
我已经把表3<#表-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)];
}
}发布于 2022-07-07 19:17:54
JavaScript不适合按位操作。我觉得你在这方面有问题
//1. Three consecutive circular shift left (rotate no carry -left) operations; and
binary = binary << 3;首先将十进制转换为二进制字符串为8位二进制,如00100100,并在数的末尾移动最多的3位,如001 00100 = 00100001。现在,您可以将返回字符串转换为十进制数。如果使用一个以上的字节,则结果等于00100100000,这是错误的。
https://stackoverflow.com/questions/47892365
复制相似问题