Hamming Code
-
Upload
roymond-chandra-pradana -
Category
Documents
-
view
1 -
download
0
description
Transcript of Hamming Code
Option Strict On02 Module Module103 Sub Main()04 Dim character As Char = "!"c, denary As Integer = 0, binary, hexadecimal, oddParity, evenParity, hamming As String05 Console.Title = "ASCII to Hamming Converter"06 Console.ForegroundColor = ConsoleColor.Green07 Console.Write("ASCII Character: ") : character = CChar(Console.ReadLine())08 Console.WriteLine("----------------------")09 10 denary = Asc(character)11 binary = Denary2BaseX(denary, 2)12 hexadecimal = Denary2BaseX(denary, 16)13 evenParity = Parity(binary, "Even")14 oddParity = Parity(binary, "Odd")15 16 'For Readability's sake...17 binary = binary.Substring(0, 3) & " " & binary.Substring(3, 4)18 evenParity = evenParity.Substring(0, 4) & " " & evenParity.Substring(4, 4)19 oddParity = oddParity.Substring(0, 4) & " " & oddParity.Substring(4, 4)20 21 Console.WriteLine("Denary: {0}", denary)22 Console.WriteLine("Hexadecimal: {0}", hexadecimal)23 Console.WriteLine("Binary: {0}", binary)24 Console.WriteLine("Even Parity: {0}", evenParity)25 Console.WriteLine("Odd Parity: {0}", oddParity)26 Console.WriteLine("Hamming: {0}", hamming)27 Console.WriteLine("")28 29 End Sub30 Public Function Denary2BaseX(ByVal i As Integer, ByVal b As Integer) As String31 Dim r As Integer = 0, n As String = "", c As String = "0123456789ABCDEF", d As Boolean = False32 Do Until i = 033 r = i Mod b34 i = i \ b35 n = c.Substring(r, 1) & n36 Loop37 Return n38 End Function39 Public Function Parity(ByVal binary As String, ByVal type As String) As String40 Dim quantityOf1s As Integer = 0, parityBit As Integer = 041 Parity = ""42 For Each character In binary 'Count the 1s43 If character = "1" Then quantityOf1s += 144 Next45 Select Case type46 Case "Even"47 If quantityOf1s Mod 2 = 0 Then parityBit = 0 Else parityBit = 148 Return parityBit & binary49 Case "Odd"50 If quantityOf1s Mod 2 = 0 Then parityBit = 1 Else parityBit = 051 Return parityBit & binary52 End Select53 End Function54 Public Function Hamming(ByVal binary As String) As String55 'To be completed!56 End Function57 End Module