###現在の状況
現在、何処かで見たプログラムを参考にC#でCRC値を計算するプログラムを作って(移植して?)使用しているのですが、数回~数十回に一回程度、途中で計算した数値や計算したCRC値が全て0になってしまいます。
エラー等は無く、CRC値を計算する元のデータ(バイト列)も取得できていて、実際に計算もできているのですが、時々計算ができないのが原因が分からず、手詰まり状態になっています。
###該当のソースコード
static UInt16 crc_calc(byte[] bytes, int calc_start_pos, int calc_length)
{
UInt16[] log = new UInt16[calc_length];
crc_reg = 0;
bool w = false;
for (int calc_byte = calc_start_pos; calc_byte < calc_length; calc_byte++)
{
log[calc_byte] = crc_reg;
for (int calc_bit = 0; calc_bit != 8; calc_bit++)
{
if ((((crc_reg >> 15) ^ ((bytes[calc_start_pos + calc_byte] << calc_bit) >> 7)) & 0x01) == 1)
{
w = true;
}
else
{
w = false;
}
crc_reg = (UInt16)((crc_reg << 1) & 0xFFFF);
if (w)
{
crc_reg = (UInt16)(crc_reg ^ 0x1021);
}
}
}
if(crc_reg ==0)
{
Console.ReadKey();
}
return crc_reg;
}
###取得したデータ例(一部)
bytes {byte[0x00010000]} byte[]
[177] 0xb0 byte
[178] 0xf0 byte
[179] 0xb0 byte
[180] 0xe3 byte
[181] 0xc7 byte
[182] 0x2f byte
[183] 0xc3 byte
[184] 0x00 byte
[185] 0x00 byte
[186] 0x34 byte
[187] 0xc3 byte
[188] 0xa5 byte
[189] 0xfd byte
[190] 0x26 byte
[191] 0xc3 byte
[192] 0x29 byte
[193] 0xed byte
[194] 0xd5 byte
[195] 0x41 byte
[196] 0x00 byte
[197] 0x00 byte
[198] 0x34 byte
[199] 0xc3 byte
[200] 0xa3 byte
[201] 0x12 byte
[202] 0x02 byte
[203] 0xc3 byte
[204] 0xff byte
[205] 0xb6 byte
[206] 0x02 byte
calc_start_pos 0x000000b1 int
calc_length 0x0000001c int
log {ushort[0x0000001c]} ushort[]
[0] 0x0000 ushort
[1] 0x0000 ushort
[2] 0x0000 ushort
[3] 0x0000 ushort
[4] 0x0000 ushort
[5] 0x0000 ushort
[6] 0x0000 ushort
[7] 0x0000 ushort
[8] 0x0000 ushort
[9] 0x0000 ushort
[10] 0x0000 ushort
[11] 0x0000 ushort
[12] 0x0000 ushort
[13] 0x0000 ushort
[14] 0x0000 ushort
[15] 0x0000 ushort
[16] 0x0000 ushort
[17] 0x0000 ushort
[18] 0x0000 ushort
[19] 0x0000 ushort
[20] 0x0000 ushort
[21] 0x0000 ushort
[22] 0x0000 ushort
[23] 0x0000 ushort
[24] 0x0000 ushort
[25] 0x0000 ushort
[26] 0x0000 ushort
[27] 0x0000 ushort
###使用しているツール
VisualStudioComunity2017 Version15.2(26430.16)
NET FrameWork Version4.7.02053
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/08/28 00:06