【发布时间】:2012-02-27 10:57:25
【问题描述】:
这是我的新代码,它将 7 位二进制数转换为具有偶校验的 8 位。但是它不起作用。例如,当我输入 0101010 时,它显示偶数奇偶校验的数字是 147。你能帮我看看有什么问题吗?
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Please enter a 7-bit binary number:");
int a = Convert.ToInt32(Console.ReadLine());
byte[] numberAsByte = new byte[] { (byte)a };
System.Collections.BitArray bits = new System.Collections.BitArray(numberAsByte);
a = a << 1;
int count = 0;
for (int i = 0; i < 8; i++)
{
if (bits[i])
{
count++;
}
if (count % 2 == 1)
{
bits[7] = true;
}
bits.CopyTo(numberAsByte, 0);
a = numberAsByte[0];
Console.WriteLine("The number with an even parity bit is:");
Console.Write(a);
Console.ReadLine();
}
}
}
【问题讨论】:
-
问题是我不知道如何使用奇偶校验和二进制,所以我需要帮助。这是我保存的工作,这并不多,因为我不知道平价。 Console.WriteLine("请输入 7 位二进制数:"); int a = Convert.ToInt32(Console.ReadLine());
-
你是指哪个奇偶校验?偶数还是奇数?
-
一个7位数字可以表示为一个7个字符的字符串,每个字符必须是
'0'或'1',也可以表示为整数类型的值范围 0-127。您以“您使用的唯一数字......”开头的句子暗示了前一种方法,但只有在您采用后一种方法时才能调用Convert.ToInt32。