Hello everyone,
The latest assignment from my teacher is to create a console program where the user enters a character and the respective
ASCII value in
Denary,
Hexadecimal and
Binary,
Even Parity,
Odd Parity and
Hamming code are returned.
I've managed to implement all of them,with relative ease, except the latter. I am familiar with the concept of
Hamming code and can compute it easily by hand, but don't really know how to implement it as a function in my program.
Here is my code thus far:
Code:
Option Strict On
Module Module1
Sub Main()
Dim character As Char = "!"c, denary As Integer = 0, binary, hexadecimal, oddParity, evenParity, hamming As String
Console.Title = "ASCII to Hamming Converter"
Console.ForegroundColor = ConsoleColor.Green
Console.Write("ASCII Character: ") : character = CChar(Console.ReadLine())
Console.WriteLine("──────────────────────")
denary = Asc(character)
binary = Denary2BaseX(denary, 2)
hexadecimal = Denary2BaseX(denary, 16)
evenParity = Parity(binary, "Even")
oddParity = Parity(binary, "Odd")
'For Readability's sake...
binary = binary.Substring(0, 3) & " " & binary.Substring(3, 4)
evenParity = evenParity.Substring(0, 4) & " " & evenParity.Substring(4, 4)
oddParity = oddParity.Substring(0, 4) & " " & oddParity.Substring(4, 4)
Console.WriteLine("Denary: {0}", denary)
Console.WriteLine("Hexadecimal: {0}", hexadecimal)
Console.WriteLine("Binary: {0}", binary)
Console.WriteLine("Even Parity: {0}", evenParity)
Console.WriteLine("Odd Parity: {0}", oddParity)
Console.WriteLine("Hamming: {0}", hamming)
Console.WriteLine("")
End Sub
Public Function Denary2BaseX(ByVal i As Integer, ByVal b As Integer) As String
Dim r As Integer = 0, n As String = "", c As String = "0123456789ABCDEF", d As Boolean = False
Do Until i = 0
r = i Mod b
i = i \ b
n = c.Substring(r, 1) & n
Loop
Return n
End Function
Public Function Parity(ByVal binary As String, ByVal type As String) As String
Dim quantityOf1s As Integer = 0, parityBit As Integer = 0
Parity = ""
For Each character In binary 'Count the 1s
If character = "1" Then quantityOf1s += 1
Next
Select Case type
Case "Even"
If quantityOf1s Mod 2 = 0 Then parityBit = 0 Else parityBit = 1
Return parityBit & binary
Case "Odd"
If quantityOf1s Mod 2 = 0 Then parityBit = 1 Else parityBit = 0
Return parityBit & binary
End Select
End Function
Public Function Hamming(ByVal binary As String) As String
'To be completed!
End Function
End Module
As usual any help will be greatly appreciated! :D