Cにこの関数があり、C#に移植する必要があります。私はいくつかの試みをしましたが、私が間違っていることを理解することができません。
多項式は0x04C11DB7uLです。
whileループを含める必要はありません。また、Forループを試してみました。
static uint32_t APPL_CalcCRC32(uint32_t u32Sum, uint8_t *pData, uint32_t count)
{
uint8_t iCounter;
uint32_t u32Word;
uint64_t u64Sum;
u64Sum = u32Sum;
count = count / 4; // number of bytes --> number of words
// algorithm according to AN4187 (Figure 2)
while (count--)
{
u32Word = *((uint32_t*)pData)++;
u64Sum ^= u32Word;
for (iCounter = 0; iCounter < (8 * 4); ++iCounter)
{
u64Sum <<= 1;
if (u64Sum & 0x100000000)
{
u64Sum ^= 0x04C11DB7uL;
}
}
}
return (uint32_t)u64Sum;
}
これは私の試みです:
private uint CalculateBlockCheckSum( uint u32Sum, byte[] blockImage )
{
uint u32Word;
ulong u64Sum = u32Sum;
ulong comparisonValue = 0x100000000;
int count = blockImage.Length / 4;
int i = 0;
while ( count-- >= 0 )
{
u32Word = blockImage[i++];
u64Sum ^= u32Word;
for ( byte iCounter = 0; iCounter < ( 8 * 4 ); ++iCounter )
{
u64Sum <<= 1;
if ( ( u64Sum & comparisonValue ) != 0 )
{
u64Sum ^= 0x04C11DB7uL;
}
}
}
return (uint)u64Sum;
}
私の主な疑問は、C#関数でのu32Wordの割り当てとループ基準ですが、正しいですか?
私のテストセットアップは58の配列(ブロック)で、各ブロックはそれぞれ1024バイトです。しかし、両方の関数の出力は同じではありません。それで、私の機能は間違っていますか、それとも何か他のものですか?
データブロックの次の値に移動するときにのみ、行を変更する必要があります。
private uint CalculateBlockCheckSum(uint u32Sum, byte[] blockImage)
{
uint u32Word;
ulong u64Sum = u32Sum;
ulong comparisonValue = 0x100000000;
int count = blockImage.Length / sizeof(uint);
int i = 0;
while (count-- > 0)
{
u32Word = BitConverter.ToUInt32(blockImage,i*sizeof(uint));
u64Sum ^= u32Word;
for (byte iCounter = 0; iCounter < (8 * 4); ++iCounter)
{
u64Sum <<= 1;
if ((u64Sum & comparisonValue) != 0)
{
u64Sum ^= 0x04C11DB7uL;
}
}
i++;
}
return (uint)u64Sum;
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加