const UInt32 poly = 0x04C11DB7;
public static UInt32 GenerateCRC_32(byte[] message)
{
byte[] augmentedMsg = new byte[message.Length + 4];
message.CopyTo(augmentedMsg, 0);
UInt32 remainder = Convert.ToUInt32(augmentedMsg[0]) << 24 |
Convert.ToUInt32(augmentedMsg[1]) << 16 |
Convert.ToUInt32(augmentedMsg[2]) << 8 |
Convert.ToUInt32(augmentedMsg[3]);
for (Int32 i = 4; i < augmentedMsg.Length; i++)
{
for (int bit = 0; bit < 8; bit++)
{
UInt32 nextBit = ((UInt32)augmentedMsg[i] >> (7 - bit)) & 0x01;
if ((remainder & 0x80000000) > 0)
{
Console.WriteLine("---------------DO XOR --------------------");
Console.WriteLine(Convert.ToString(((remainder << 1) | nextBit), 2).PadLeft(32, '0'));
Console.WriteLine(Convert.ToString(poly, 2).PadLeft(32, '0'));
Console.WriteLine("------------------------------------------");
remainder = ((remainder << 1) | nextBit) ^ poly;
Console.WriteLine(Convert.ToString(remainder, 2).PadLeft(32, '0'));
Console.WriteLine("------------------------------------------");
}
else
{
remainder = (remainder << 1) | nextBit;
Console.WriteLine("--------------NO---------------------");
Console.WriteLine(Convert.ToString(remainder, 2).PadLeft(32, '0'));
Console.WriteLine("------------------------------------------");
}
}
}
Console.WriteLine(Convert.ToString(remainder, 2).PadLeft(32, '0'));
Console.WriteLine(remainder.ToString("X"));
return remainder;
}
输入消息:'A'(十六进制:0x41)输出:0x30476DC0根据本网站:输出应为:0xD3D99E8B
我想我错过了CRC的反转/初始化,但我不确定如何改变这个基本实现来获得与网站结果等效的结果。
运行我的程序时的输出:
--------------NO---------------------
10000010000000000000000000000000
------------------------------------------
---------------DO XOR --------------------
00000100000000000000000000000000
00000100110000010001110110110111
------------------------------------------
00000000110000010001110110110111
------------------------------------------
--------------NO---------------------
00000001100000100011101101101110
------------------------------------------
--------------NO---------------------
00000011000001000111011011011100
------------------------------------------
--------------NO---------------------
00000110000010001110110110111000
------------------------------------------
--------------NO---------------------
00001100000100011101101101110000
------------------------------------------
--------------NO---------------------
00011000001000111011011011100000
------------------------------------------
--------------NO---------------------
00110000010001110110110111000000
------------------------------------------
00110000010001110110110111000000
>
对最终值进行异或,与XORed值相反
public static UInt32 GenerateCRC_32(Byte[]message){byte[]augmentedMsg=new byte[message.length+8];message.copyto(augmentedMsg,4);//修改以创建初始化空间
UInt32 remainder = Convert.ToUInt32(augmentedMsg[0]) << 24 |
Convert.ToUInt32(augmentedMsg[1]) << 16 |
Convert.ToUInt32(augmentedMsg[2]) << 8 |
Convert.ToUInt32(augmentedMsg[3]);
remainder = ~remainder; // Overwrite the above and initialized the register to 0xFFFFFFFF
for (Int32 i = 4; i < augmentedMsg.Length; i++)
{
byte reversedMessage = Reverse(augmentedMsg[i]); // Reversed the augmented message byte
for (int bit = 0; bit < 8; bit++)
{
UInt32 nextBit = Convert.ToUInt32(reversedMessage >> (7 - bit)) & 0x1; // Use the reversed message byte
if ((remainder & 0x80000000) > 0)
{
Console.WriteLine("---------------DO XOR --------------------");
Console.WriteLine(Convert.ToString(((remainder << 1) | nextBit), 2).PadLeft(32, '0'));
Console.WriteLine(Convert.ToString(poly32, 2).PadLeft(32, '0'));
Console.WriteLine("------------------------------------------");
remainder = Convert.ToUInt32((UInt32)((UInt32)(remainder << 1) | nextBit) ^ poly32);
Console.WriteLine(Convert.ToString(remainder, 2).PadLeft(32, '0'));
Console.WriteLine("------------------------------------------");
}
else
{
remainder = (UInt32)((UInt32)(remainder << 1) | nextBit);
Console.WriteLine("--------------NO---------------------");
Console.WriteLine(Convert.ToString(remainder, 2).PadLeft(32, '0'));
Console.WriteLine("------------------------------------------");
}
}
}
Console.WriteLine(Convert.ToString(remainder, 2).PadLeft(32, '0') + "(" + remainder.ToString("X") + ")");
remainder = (~remainder);
Console.WriteLine("XOR ^ 0xFFFFFFFF : " + Convert.ToString(remainder, 2).PadLeft(32, '0') + "(" + remainder.ToString("X") + ")");
remainder = Reverse(remainder);
Console.WriteLine("Reversed the Abv : " + Convert.ToString(remainder, 2).PadLeft(32, '0') + "(" + remainder.ToString("X") + ")");
return remainder;
}
---------------DO XOR --------------------
11111111111111111111111111111111
00000100110000010001110110110111
------------------------------------------
11111011001111101110001001001000
------------------------------------------
---------------DO XOR --------------------
11110110011111011100010010010000
00000100110000010001110110110111
------------------------------------------
11110010101111001101100100100111
------------------------------------------
---------------DO XOR --------------------
11100101011110011011001001001110
00000100110000010001110110110111
------------------------------------------
11100001101110001010111111111001
------------------------------------------
---------------DO XOR --------------------
11000011011100010101111111110010
00000100110000010001110110110111
------------------------------------------
11000111101100000100001001000101
------------------------------------------
---------------DO XOR --------------------
10001111011000001000010010001010
00000100110000010001110110110111
------------------------------------------
10001011101000011001100100111101
------------------------------------------
---------------DO XOR --------------------
00010111010000110011001001111010
00000100110000010001110110110111
------------------------------------------
00010011100000100010111111001101
------------------------------------------
--------------NO---------------------
00100111000001000101111110011011
------------------------------------------
--------------NO---------------------
01001110000010001011111100110110
------------------------------------------
--------------NO---------------------
10011100000100010111111001101100
------------------------------------------
---------------DO XOR --------------------
00111000001000101111110011011000
00000100110000010001110110110111
------------------------------------------
00111100111000111110000101101111
------------------------------------------
--------------NO---------------------
01111001110001111100001011011110
------------------------------------------
--------------NO---------------------
11110011100011111000010110111100
------------------------------------------
---------------DO XOR --------------------
11100111000111110000101101111000
00000100110000010001110110110111
------------------------------------------
11100011110111100001011011001111
------------------------------------------
---------------DO XOR --------------------
11000111101111000010110110011110
00000100110000010001110110110111
------------------------------------------
11000011011111010011000000101001
------------------------------------------
---------------DO XOR --------------------
10000110111110100110000001010010
00000100110000010001110110110111
------------------------------------------
10000010001110110111110111100101
------------------------------------------
---------------DO XOR --------------------
00000100011101101111101111001010
00000100110000010001110110110111
------------------------------------------
00000000101101111110011001111101
------------------------------------------
--------------NO---------------------
00000001011011111100110011111010
------------------------------------------
--------------NO---------------------
00000010110111111001100111110100
------------------------------------------
--------------NO---------------------
00000101101111110011001111101000
------------------------------------------
--------------NO---------------------
00001011011111100110011111010000
------------------------------------------
--------------NO---------------------
00010110111111001100111110100000
------------------------------------------
--------------NO---------------------
00101101111110011001111101000000
------------------------------------------
--------------NO---------------------
01011011111100110011111010000000
------------------------------------------
--------------NO---------------------
10110111111001100111110100000000
------------------------------------------
---------------DO XOR --------------------
01101111110011001111101000000000
00000100110000010001110110110111
------------------------------------------
01101011000011011110011110110111
------------------------------------------
--------------NO---------------------
11010110000110111100111101101110
------------------------------------------
---------------DO XOR --------------------
10101100001101111001111011011100
00000100110000010001110110110111
------------------------------------------
10101000111101101000001101101011
------------------------------------------
---------------DO XOR --------------------
01010001111011010000011011010110
00000100110000010001110110110111
------------------------------------------
01010101001011000001101101100001
------------------------------------------
--------------NO---------------------
10101010010110000011011011000010
------------------------------------------
---------------DO XOR --------------------
01010100101100000110110110000100
00000100110000010001110110110111
------------------------------------------
01010000011100010111000000110011
------------------------------------------
--------------NO---------------------
10100000111000101110000001100110
------------------------------------------
---------------DO XOR --------------------
01000001110001011100000011001100
00000100110000010001110110110111
------------------------------------------
01000101000001001101110101111011
------------------------------------------
--------------NO---------------------
10001010000010011011101011110110
------------------------------------------
---------------DO XOR --------------------
00010100000100110111010111101100
00000100110000010001110110110111
------------------------------------------
00010000110100100110100001011011
------------------------------------------
--------------NO---------------------
00100001101001001101000010110110
------------------------------------------
--------------NO---------------------
01000011010010011010000101101100
------------------------------------------
--------------NO---------------------
10000110100100110100001011011000
------------------------------------------
---------------DO XOR --------------------
00001101001001101000010110110000
00000100110000010001110110110111
------------------------------------------
00001001111001111001100000000111
------------------------------------------
--------------NO---------------------
00010011110011110011000000001110
------------------------------------------
--------------NO---------------------
00100111100111100110000000011100
------------------------------------------
00100111100111100110000000011100(279E601C)
XOR ^ 0xFFFFFFFF : 11011000011000011001111111100011(D8619FE3)
Reversed the Abv : 11000111111110011000011000011011(C7F9861B)
public static UInt32 GenerateCRC_32_from_Table(byte[] message)
{
byte[] augmentedMsg = new byte[message.Length + 4];
message.CopyTo(augmentedMsg, 0);
UInt32 remainder = 0xFFFFFFFF;
foreach (byte msgByte in augmentedMsg)
{
byte reversedMsgByte = Reverse(msgByte);
remainder = ((remainder << 8) | Convert.ToUInt32(reversedMsgByte)) ^ crc32_table[((remainder >> 24)) & 0xFF];
}
remainder = Reverse(~remainder);
return remainder;
}
public static UInt32 GenerateCRC_32_from_Table(byte[] message)
{
UInt32 remainder = 0xFFFFFFFF;
foreach (byte msgByte in message)
{
byte reversedMsgByte = Reverse(msgByte);
remainder = (remainder << 8) ^ crc32_table[((remainder >> 24) ^ Convert.ToUInt32(reversedMsgByte)) & 0xFF];
}
remainder = Reverse(~remainder);
return remainder;
}
Reverse()和poly32如注释中所述:**
const UInt32 poly32 = 0x04C11DB7;
public static UInt32 Reverse(UInt32 message)
{
UInt32 msgReversed = 0;
for (int i = 0; i < 32; i++)
{
msgReversed = ((message & 0x80000000) >> (31 - i)) | msgReversed;
message = message << 1;
}
return msgReversed;
}
public static byte Reverse(byte message)
{
byte msgReversed = 0;
for (int i = 0; i < 8; i++)
{
msgReversed = (byte)(((byte)((byte)(message) & 0x80) >> (7 - i)) | msgReversed);
message = (byte)(message << 1);
}
return msgReversed;
}
您所引用的标准CRC被反映出来,即位被反向,并且它被初始化为0xffffffff
,最后的CRC是用0xffffffff
异或。
您生成的CRC位不反转,初始CRC为零,结尾没有异或。
通常,为了实现位反转CRC,您保留输入字节不变,但反映多项式并向另一个方向移动。输入字节被馈送到CRC的底部而不是顶部。这还将通过增强消息方法处理的总字节数减少了4个。
相反,应该做的是,甚至不要试图增加消息,并排除-或消息到CRC的顶部,而不是试图将消息输入到CRC的底部。
此外,反转CRC,应用反转的消息,然后再次反转CRC等价于不做这些,而是反转多项式并在另一方向上移动。多项式是一个常数,所以在编写代码时要进行反向操作,正如我最初的答案中所指出的那样。0x04c11db7
反转为0xedb88320
。
因此,代码最终如下所示(在C语言中):
#include <stddef.h>
#include <stdint.h>
/* CRC-32 (Ethernet, ZIP, etc.) polynomial in reversed bit order. */
#define POLY 0xedb88320
/* Compute CRC of buf[0..len-1] with initial CRC crc. This permits the
computation of a CRC by feeding this routine a chunk of the input data at a
time. The value of crc for the first chunk should be zero. */
uint32_t crc32(uint32_t crc, const unsigned char *buf, size_t len)
{
int k;
crc = ~crc;
while (len--) {
crc ^= *buf++;
for (k = 0; k < 8; k++)
crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1;
}
return ~crc;
}
维基百科离线阅读器,支持索引官方dump!
问题内容: 我需要获取任何电影的信息框的内容。我知道电影的名字。一种方法是获取Wikipedia页面的完整内容,然后解析它,直到找到并获取信息框的内容。 使用某些API或解析器,是否还有其他方法可以实现? 我正在使用Python和pywikipediabot API。 我也熟悉wikitools API。因此,如果有人具有与wikitools API相关的解决方案,请不要使用pywikipedia
问题内容: 例如,使用此Wikipedia转储: http://en.wikipedia.org/w/api.php?action=query&prop=revisions&titles=lebron%20james&rvprop=content&redirects=true&format=xmlfm 我是否可以使用现有的Python库来创建带有主题和值映射的数组? 例如: 问题答案: 看来您确实
我正在尝试从这个维基百科页面中获取表数据:https://en.wikipedia.org/wiki/2020_coronavirus_pandemic_in_Nepal我试过用熊猫警犬。Read Office HTML语法,但它对我试图擦除的表无效(证实了CavID-19在尼泊尔地区的病例)。 我试着用Beautifulsoup和pandas来搜集数据,但没有用
有办法从维基百科获得所有标题/摘录对吗?到那一刻,我发现了两种方法: 下载摘录转储,但它包含不完整/无效的摘录,我想是作为文章的第一行。 使用MediaWiki API请求摘录,但它非常慢,因为每个请求只能获得单个摘录(批量查询不适用于摘录): /w/api.php?action=query 我想获得摘录,因为它们是由MediaWiki API生成的,而不需要负担维基百科服务器。可能吗? 顺便说一
在本章中,我展示了上一个练习的解决方案,并分析了 Web 索引算法的性能。然后我们构建一个简单的 Web 爬虫。 15.1 基于 Redis 的索引器 在我的解决方案中,我们在 Redis 中存储两种结构: 对于每个检索词,我们有一个URLSet,它是一个 Redis 集合,包含检索词的 URL。 对于每个网址,我们有一个TermCounter,这是一个 Redis 哈希表,将每个检索词映射到它出