控制器应用程序使用汉明算法的代码错误查找器

时间:2010-11-10 14:20:31

标签: hamming-code

我需要一个用于字节校正的控制台应用程序,使用汉明算法。有人可以帮我解决这个问题吗?

输入单词例如:11100100

2 个答案:

答案 0 :(得分:1)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace Bits
{
    class Program
    {
        static void Main(string[] args)
        {
            string NrBinary;
            int pp, Key, number;
            double x, y;
            Console.WriteLine("Give binary value till 8 bit: ");
            NrBinary = Console.ReadLine();
            if (NrBinary.Length <= 8)
            {
                if (NrBinary.Length == 1) NrBinary = "0000000" + NrBinary;
                if (NrBinary.Length == 2) NrBinary = "000000" + NrBinary;
                if (NrBinary.Length == 3) NrBinary = "00000" + NrBinary;
                if (NrBinary.Length == 4) NrBinary = "0000" + NrBinary;
                if (NrBinary.Length == 5) NrBinary = "000" + NrBinary;
                if (NrBinary.Length == 6) NrBinary = "00" + NrBinary;
                if (NrBinary.Length == 7) NrBinary = "0" + NrBinary;


                pp = NrBinary.Length;
                number = Convert.ToInt32(NrBinary);
                Console.WriteLine("PP = " + pp);

                for (Key = 0; Key < pp; ++Key)
                {
                    x = Math.Pow(2, Key) - 1;
                    y = pp + Key;

                    if (x >= y)
                    {
                        goto Mess;
                    }
                }
            Mess:
                Console.WriteLine("Controled bit needed Key = " + Key + "\n");

                int[] parity = new int[pp];
                int[] DI = new int[pp];
                int[] CI = new int[Key];

                DI[0] = number % 10;
                DI[1] = (number / 10) % 10;
                DI[2] = (number / 100) % 10;
                DI[3] = (number / 1000) % 10;
                DI[4] = (number / 10000) % 10;
                DI[5] = (number / 100000) % 10;
                DI[6] = (number / 1000000) % 10;
                DI[7] = (number / 10000000) % 10;

                CI[0] = DI[0] ^ DI[1] ^ DI[3] ^ DI[4] ^ DI[6];
                CI[1] = DI[0] ^ DI[2] ^ DI[3] ^ DI[5] ^ DI[6];
                CI[2] = DI[1] ^ DI[2] ^ DI[3] ^ DI[7];
                CI[3] = DI[4] ^ DI[5] ^ DI[6] ^ DI[7];

                Console.WriteLine("\n  = " + CI[0] + "" + CI[1] + "" + CI[2] + "" + CI[3] + "\n");
                Console.Write("Bit with Hamming code: ");
                Console.WriteLine(DI[7] + "" + DI[6] + "" + DI[5] + "" + DI[4] + "" + CI[3] + "" + DI[3]
                + "" + DI[2] + "" + DI[1] + "" + CI[2] + "" + DI[0] + "" + CI[1] + "" + CI[0]);
            }
            else
            {
                Console.WriteLine("\n\n 8 bit character maximaly:)");
            }
        }
    }
}

答案 1 :(得分:0)

假设这是家庭作业,这里是pointer to the general algorithm。自己动手,发布你尝试过的内容以及你遇到的具体问题,人们可能会帮助你。