C 中的 XOR 类型场景输出不正确
基本上我正在制作一个程序,根据初始十六进制值评估 CRC。在这种情况下:
Hex = AB1245
我已在程序中将其转换为二进制:
1010 1011 0001 0010 0100 0101
这是具有给定多项式的 CRC-8:
1 1111 1011
到目前为止,我已使用此信息创建了以下设置:
1010 1011 0001 0010 0100 0101 0000 0000
1111 1101 1000 0000 0000 0000 0000 0000
其中这两个字符串应该进行异或并生成一个新的字符串将产生字符串。它应该是:
0101 0110 1001 0010 0100 0101 0000 0000
我的代码是这样的:
for(i = 0; i < strlen(binary); i++) {
if (binary[i] == polynomial[i])
strcat(binary2, "0");
else
strcat(binary2, "1");
}
其中,binary 是
1010 1011 0001 0010 0100 0101
,多项式是
1111 1101 1000 0000 0000 0000 0000 0000
,binary2
是应该通过异或运算创建的新二进制字符串。
我的问题是,每当我运行这个程序时,它直到最后 4 位都是正确的,这使得它们无缘无故地全部为 1。所以我的输出是:
0101 0110 1001 0010 0100 0101 0000 1111
最后四位应该是 0,直到多项式字符串被推到那么远。我完全不知道为什么会发生这种情况。任何人都可以透露一些信息吗?
编辑:根据要求提供更多代码。
binary
初始化为:
char binary[4*strlen(hex)];
其中 hex 是十六进制值的字符串 AB1245
下一位是用于创建多项式并将 0 位附加到 末尾的过程>binary
(CRC 计算所需):
char polynomial[10] = "111111011";
// Append the necessary number of bits to the binary string
int track = strlen(binary);
strcat(binary, "00000000"); //This is static as 8 zeroes are always used in CRC-8
// Create the polynomial w/ tail for the XOR steps
char tail[strlen(binary)];
strcpy(tail, "0"); //For some reason if I don't do this it freaks out
for (i=0; i < strlen(binary)-strlen(polynomial); i++)
strcat(tail, "0");
strcat(polynomial, tail);
此时我可以通过在两者上打印 strlen()
的结果来验证它们的长度相同。它们都正确地输出为 32
。
Basically I am making a program that is evaluating CRC on an initial hex value. In this case:
Hex = AB1245
I have converted it to binary in the program:
1010 1011 0001 0010 0100 0101
This is CRC-8 with a given polynomial:
1 1111 1011
So far, I have used this information to create the following setup:
1010 1011 0001 0010 0100 0101 0000 0000
1111 1101 1000 0000 0000 0000 0000 0000
Where those two strings should be XOR'd and a new string would be produced. It should be:
0101 0110 1001 0010 0100 0101 0000 0000
My code is as such:
for(i = 0; i < strlen(binary); i++) {
if (binary[i] == polynomial[i])
strcat(binary2, "0");
else
strcat(binary2, "1");
}
Where binary is
1010 1011 0001 0010 0100 0101
, polynomial is
1111 1101 1000 0000 0000 0000 0000 0000
and binary2
is the new binary string that should be created from them being XOR'd.
My problem is whenever I run this program, it is correct up to the last 4 bits, where it makes them all 1
s for no reason. So my output is:
0101 0110 1001 0010 0100 0101 0000 1111
The last four bits should be 0 until the polynomial string gets pushed over that far. I am totally out of ideas as to why this is happening. Can anyone shed some light?
EDIT: More code as requested.
binary
is initiated as:
char binary[4*strlen(hex)];
where hex is the string of hexidecimal values AB1245
The next bit is the process used to both create polynomial and append the 0 bits to the end of binary
(needed for CRC calculation):
char polynomial[10] = "111111011";
// Append the necessary number of bits to the binary string
int track = strlen(binary);
strcat(binary, "00000000"); //This is static as 8 zeroes are always used in CRC-8
// Create the polynomial w/ tail for the XOR steps
char tail[strlen(binary)];
strcpy(tail, "0"); //For some reason if I don't do this it freaks out
for (i=0; i < strlen(binary)-strlen(polynomial); i++)
strcat(tail, "0");
strcat(polynomial, tail);
At this point I can verify they are the same length by printing the result of the strlen()
on both of them. They both correctly come out to 32
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来您正在堆栈上分配字符串,其长度是根据以类似方式分配的其他字符串计算得出的。这带来了两个主要问题:
首先,字符数组不会自动增长以适应您放入其中的任何内容,并且它们的大小必须调整为在整个文本后面包含一个以空结尾的字节。因此,二进制和多项式应声明为 char binary[33]; 甚至超过 33,具体取决于其他可能的输入。如果您希望它们“增长”,那么您将需要使用
malloc
来动态分配它们。 这很可能是导致问题的原因,您至少溢出了一个甚至可能是多个数组。第二,char 数组必须初始化为空,默认情况下它们不为空(它们包含“垃圾”),因此计算它们的长度或连接它们会产生意想不到的结果。
It looks like you're allocating your strings on the stack with lengths calculated from other strings allocated in a similar manner. This presents two main problems:
First, char arrays will not automatically grow to fit whatever you put in them, and they must be sized to include a null terminated byte after the entire text. So binary and polynomial should be declared as
char binary[33];
or even more than 33 depending on what other inputs are possible. If you want them to "grow" then you will need to usemalloc
to dynamically allocate them. This is most likely the cause of your problems, you're overflowing at least one and probably several of your arrays.Second, char arrays must be initialized to be empty, they are not empty by default (they contain "garbage") and so taking their length or concatenating to them will produce unexpected results.