Wrox Programmer Forums
|
Visual Basic 2010 General Discussion For any discussions about Visual Basic 2010 topics which aren't related to a specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual Basic 2010 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 21st, 2010, 08:25 PM
Authorized User
 
Join Date: Oct 2010
Posts: 29
Thanks: 23
Thanked 0 Times in 0 Posts
Send a message via MSN to SamC
Question Hamming Code

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
__________________
"Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction."
- Albert Einstein
 
Old November 21st, 2010, 11:06 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Ummmm...your parity values are off.

Unless you are considering all characters as only 7 bits.

For that matter, I think your Denary2BaseX is flawed.

Given, for example, a SPACE character as input, you will produce an output of "100000" ... only 6 bits.

I think you should instead *assume* your values are 8 bits long and produce the requisite number of digits (in whatever base) to cover 8 bits.

And so then when you calculate parity you would get 9 bits.
 
Old November 21st, 2010, 11:06 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Regarding computing the Hamming code: Write out the steps you would do by hand. Translate those steps into a program.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Code to save html page source code? Jaymond Flurrie Access VBA 2 July 2nd, 2019 06:04 PM
Double clicking control does not add code to Code Behind Paul Walton BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 5 September 25th, 2009 05:40 PM
Urgent:hard disk serial code and vb code ivanlaw Pro VB 6 0 July 25th, 2007 04:05 AM
VB: .Exe file, serial code and activation code ivanlaw Pro VB 6 8 July 6th, 2007 05:44 AM
code clinic - Why wont example asp code work? jardbf Classic ASP Basics 3 April 27th, 2006 06:22 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.