hamming-code

Console application using Hamming algorithm for code bug finder


I need a console application for byte correction, using Hamming algorithm. Can anybody help me on this issue?

Input word would be for example: 11100100


Solution

  • 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:)");
                }
            }
        }
    }