英文:
how can i make a CRC in typescript or javascript
问题
我有一个需求,当用户发送十六进制数组时,如[01 06 00 01 00 10]
除数是'11000000000000101'
谁知道如何做或给我一个在js或ts中的示例。
英文:
now, i have a need when user send Hexadecimal array like [01 06 00 01 00 10]
the Divisor is '11000000000000101'
who knows how to do it or give me a example in js or ts
答案1
得分: 1
需要更多信息才能回答你的问题。CRC不仅由多项式定义(你已经提供了一个常见的16位CRC多项式,0x8005
或 x16+x15+x2+1),还受到从字节中提供给CRC的比特顺序、CRC寄存器的初始值和从CRC生成结果的比特顺序的影响。你可能还需要了解16位CRC中两个字节的顺序是按照消息中的顺序(小端或大端)进行排列。
从已知CRC列表中,我看到有七种不同的16位CRC定义,它们使用相同的多项式,但有不同的比特顺序、初始值和最终CRC值的选择。
你需要找到并提供这些信息,或者至少提供一些消息及其CRC值的示例,以尝试推导其定义。
英文:
More information is needed to answer your question. A CRC is defined not just by the polynomial (for which you have provided a common 16-bit CRC polynomial, 0x8005
or x<sup>16</sup>+x<sup>15</sup>+x<sup>2</sup>+1) but also the order in which the bits from the bytes are fed to the CRC, the initial value of the CRC register, and the order of the bits from the CRC to make the result. You may also need to know the order of the two bytes of the 16-bit CRC as they are placed in the message (little or big endian).
From a list of known CRCs, I see seven different 16-bit CRC definitions that use that polynomial, with various choices of bit orderings, initial values and final CRCs.
You would need to find and provide that information, or at least provide several examples of messages and their CRCs in order to try to derive its definition.
答案2
得分: -2
这是一个在JavaScript中的函数示例,它接受一个十六进制数组和一个除数,然后返回由数组表示的十六进制数字除以除数后的余数:
function divideHexArray(hexArray, divisor) {
let hexNumber = "";
// 将十六进制数组转换为十六进制字符串
for (let i = 0; i < hexArray.length; i++) {
hexNumber += hexArray[i].toString(16).padStart(2, "0");
}
// 将十六进制字符串转换为十进制数字
let decimalNumber = parseInt(hexNumber, 16);
// 将十进制数字除以除数,然后返回余数
return decimalNumber % divisor;
}
请注意,这是一个JavaScript函数的示例,用于处理十六进制数值和计算余数。
英文:
Here's an example of a function in JavaScript that takes in a hexadecimal array and a divisor and returns the remainder when the hexadecimal number represented by the array is divided by the divisor:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function divideHexArray(hexArray, divisor) {
let hexNumber = "";
// convert hexadecimal array to hexadecimal string
for (let i = 0; i < hexArray.length; i++) {
hexNumber += hexArray[i].toString(16).padStart(2, "0");
}
// convert hexadecimal string to decimal number
let decimalNumber = parseInt(hexNumber, 16);
// divide decimal number by divisor and return the remainder
return decimalNumber % divisor;
}
<!-- end snippet -->
<!-- begin snippet: js hide: false console: true babel: false -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论