c - 输出与输入 c 不同
问题描述
所以我一直在尝试用 c 奇偶校验位做一些事情。但由于某种原因,输出与输入不同。输入为:0000000 0061 0000001 输出为:0000000 7f7f 0000002
由于某种原因,输出比输入高一个,我不知道为什么会发生这种情况,因为我使用相同的代码来获取高奇偶校验 + d0, d1, d2, d3
有人可以告诉我我的代码有什么问题以及如何解决这个问题吗?
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
FILE *inputFile, *outputFile;
char buffer[100];
int res;
int charFile;
int byte;
int d0, d1, d2, d3;
int p0, p1, p2;
int highNibble; //for getting the high nibble
int lowNibble; //for getting the low nibble
if (argc == 3)
{
printf("The first argument is %s\n", argv[0]);
printf("The first argument is %s\n", argv[1]);
printf("The first argument is %s\n", argv[2]);
inputFile = fopen(argv[1], "r"); //opening the .txt file and reading it
outputFile = fopen(argv[2], "w"); //opening the .txt file and writing to it
if (inputFile == NULL)
{
printf("There has been a problem while opening the file %s\n", argv[1]); //show this when a error occus while opening the .txt
}
else if (outputFile == NULL)
{
printf("There has been a problem while writing to the file %s\n", argv[2]); //show this when a error occus while opening the .txt
}
else
{
res = fread(buffer, sizeof(char), 1, inputFile);
while (res != 0) //Found this function on the internet
{
printf("a\n");
charFile = fgetc(inputFile); //Used to obtain the input form a file as a single character at a time.
byte = (int)charFile;
//get high nibble
highNibble = (byte >> 4) & 0x0f;
//get low nibble
lowNibble = (byte & 0x0f);
//d0, d1, d2, d3 + high nibble
d0 = (highNibble >> 0) & 0b0001;
d1 = (highNibble >> 1) & 0b0001;
d2 = (highNibble >> 2) & 0b0001;
d3 = (highNibble >> 3) & 0b0001;
//p0, p1, p2
p0 = (d0 + d1 + d2) % 2;
p1 = (d0 + d1 + d3) % 2;
p2 = (d1 + d2 + d3) % 2;
uint8_t finalByteParityHigh = 0;
// Add p0
finalByteParityHigh = p0 << 0;
// Add p1
finalByteParityHigh |= p1 << 1;
// Add p2
finalByteParityHigh |= p2 << 2;
// Add d0
finalByteParityHigh |= d0 << 3;
// Add d1
finalByteParityHigh |= d1 << 4;
// Add d2
finalByteParityHigh |= d2 << 5;
// Add d3
finalByteParityHigh |= d3 << 6;
fputc(finalByteParityHigh, outputFile); //High comes first in the table
//do, d1, d2, d3 + low nibble
d0 = (lowNibble >> 0) & 0b0001;
d1 = (lowNibble >> 1) & 0b0001;
d2 = (lowNibble >> 2) & 0b0001;
d3 = (lowNibble >> 3) & 0b0001;
//p0, p1, p2
p0 = (d0 + d1 + d2) % 2;
p1 = (d0 + d1 + d3) % 2;
p2 = (d1 + d2 + d3) % 2;
uint8_t finalByteParityLow = 0;
// Add p0
finalByteParityLow = p0 << 0;
// Add p1
finalByteParityLow |= p1 << 1;
// Add p2
finalByteParityLow |= p2 << 2;
// Add d0
finalByteParityLow |= d0 << 3;
// Add d1
finalByteParityLow |= d1 << 4;
// Add d2
finalByteParityLow |= d2 << 5;
// Add d3
finalByteParityLow |= d3 << 6;
fputc(finalByteParityLow, outputFile); //Low comes second in the table
}
}
fclose(inputFile); //close the input file
fclose(outputFile); //close the output ifle
}
else
{
printf("you need to give 3 arguments");
}
}
解决方案
推荐阅读
- excel - 每当答案为零时,我需要索引函数返回空白
- function - 如何使用函数将列表中的每个元素除以 int
- python - 如何通过自动下载链接使用 Python 访问 PDF 文件?
- node.js - 跟踪用户登录次数的最佳方法
- windows - 如果笔记本电脑使用交流电源且未锁定,则用于按键的 VBscript
- r - 如何计算多次观察到的单位的斜率,不包括0
- r - 如何在R中为以下数据创建直方图
- python - pytorch中两个一维信号之间的可微卷积
- python-3.x - Pix2Pix-like GAN 中的多类分割
- json - 将应用程序属性文件中的 json 列表读取到对象列表中