我被困试图为一个互操作性项目使用javascript模仿Java代码。
有人可以帮我将上述Java转换为JavaScript代码吗?
completeWithLengthAndCrc包
public byte[] completeWithLengthAndCrc(byte[] bArr) {
if (bArr == null || bArr.length < 9) {
throw new RequestException("Length of byte array is too small ");
}
try {
int length = (bArr.length + 2) - 4;
ByteBuffer wrap = ByteBuffer.wrap(bArr);
wrap.order(ByteOrder.LITTLE_ENDIAN);
wrap.putShort(2, (short) length);
wrap.put(9, Crc.calculateCRC8(bArr, 1, 8));
byte calculateCRC8 = Crc.calculateCRC8(bArr, 1, bArr.length - 1);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byteArrayOutputStream.write(bArr);
byteArrayOutputStream.write(calculateCRC8);
byteArrayOutputStream.write(3);
return byteArrayOutputStream.toByteArray();
} catch (IOException e) {
throw new PacketRequestException((Throwable) e);
}
}
CRC包
package com.bdo.dss.amc.blelib.bluetooth.hcp.utils;
import com.google.common.base.Ascii;
import com.google.common.primitives.SignedBytes;
import org.bouncycastle.crypto.signers.PSSSigner;
public class Crc {
private static final byte[] CRC8_DATA = {0, 94, PSSSigner.TRAILER_IMPLICIT, -30, 97, 63, -35, -125, -62, -100, 126, 32, -93, -3, Ascii.f68US, 65, -99, -61, 33, Byte.MAX_VALUE, -4, -94, SignedBytes.MAX_POWER_OF_TWO, Ascii.f64RS, 95, 1, -29, -67, 62, 96, -126, -36, 35, 125, -97, -63, 66, Ascii.f59FS, -2, -96, -31, -65, 93, 3, Byte.MIN_VALUE, -34, 60, 98, -66, -32, 2, 92, -33, -127, 99, 61, 124, 34, -64, -98, Ascii.f60GS, 67, -95, -1, 70, Ascii.CAN, -6, -92, 39, 121, -101, -59, -124, -38, 56, 102, -27, -69, 89, 7, -37, -123, 103, 57, -70, -28, 6, 88, Ascii.f57EM, 71, -91, -5, 120, 38, -60, -102, 101, 59, -39, -121, 4, 90, -72, -26, -89, -7, Ascii.ESC, 69, -58, -104, 122, 36, -8, -90, 68, Ascii.SUB, -103, -57, 37, 123, 58, 100, -122, -40, 91, 5, -25, -71, -116, -46, 48, 110, -19, -77, 81, Ascii.f65SI, 78, 16, -14, -84, 47, 113, -109, -51, 17, 79, -83, -13, 112, 46, -52, -110, -45, -115, 111, 49, -78, -20, Ascii.f66SO, 80, -81, -15, 19, 77, -50, -112, 114, 44, 109, 51, -47, -113, Ascii.f58FF, 82, -80, -18, 50, 108, -114, -48, 83, Ascii.f56CR, -17, -79, -16, -82, 76, Ascii.DC2, -111, -49, 45, 115, -54, -108, 118, 40, -85, -11, Ascii.ETB, 73, 8, 86, -76, -22, 105, 55, -43, -117, 87, 9, -21, -75, 54, 104, -118, -44, -107, -53, 41, 119, -12, -86, 72, Ascii.SYN, -23, -73, 85, Ascii.f69VT, -120, -42, 52, 106, 43, 117, -105, -55, 74, Ascii.DC4, -10, -88, 116, 42, -56, -106, Ascii.NAK, 75, -87, -9, -74, -24, 10, 84, -41, -119, 107, 53};
private Crc() {
}
public static byte calculateCRC8(byte[] bArr, int i, int i2) {
byte b = 0;
while (i <= i2) {
b = CRC8_DATA[(b ^ bArr[i]) & 255];
i++;
}
return b;
}
public static byte calculateCRC8(byte[] bArr) {
return calculateCRC8(bArr, 0, bArr.length - 1);
}
}