LRC Computation Help

Good day.

I am searching for a function in VB.NET to get the LRC for a HEX result.

For instance, I have the following HEX values (I included spaces for reability only):

01 30 02 42 20 21 20 03 The LRC result is 53

what kind of VB.NET function can be written to computer this LRC?

Thank you for your help.




LVL 1
freezingHotAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jeff CertainCommented:
If you explain LRC, we'll find something :)
GivenRandyCommented:
Are your hex values in a string? An array? A file?
freezingHotAuthor Commented:
LRC = Longitudinal Redundancy Check

The hex values are in a string which are received from a COM port.
Why Diversity in Tech Matters

Kesha Williams, certified professional and software developer, explores the imbalance of diversity in the world of technology -- especially when it comes to hiring women. She showcases ways she's making a difference through the Colors of STEM program.

GivenRandyCommented:
Something like the following (but I get 33 as a result):

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ' 01 30 02 42 20 21 20 03 The LRC result is 53
        Dim MyString As String
        MyString = Chr(1)
        MyString &= Chr(30)
        MyString &= Chr(2)
        MyString &= Chr(42)
        MyString &= Chr(20)
        MyString &= Chr(21)
        MyString &= Chr(20)
        MyString &= Chr(3)
        MessageBox.Show("Result is: " & CalculateLRC(MyString))
    End Sub

    Public Function CalculateLRC(ByVal S As String) As Byte
        CalculateLRC = 0
        For Index As Integer = 1 To Len(S)
            CalculateLRC = CByte(CalculateLRC Xor Asc(Mid(S, Index, 1)))
        Next
    End Function
freezingHotAuthor Commented:
hmmmm...

00000001 (01)
00110000 (30)
00000010 (02)
01000010 (42)
00100000 (20)
00100001 (21)
00100000 (20)
00000011 (03)
-----------
01010011 = 53
Jeff CertainCommented:
Try this...

Function getLRC(message As String) As String
    Dim i As Integer
    Dim mlength As Integer
    Dim lrc As Integer
    Dim cchar As String
    lrc = 0
    i = 1 'set start position
   
    mlenght = Len(message)
   
    Do While i <= mlenght
        cchar = (Mid$(message, i, 1))
        lrc = Asc(cchar) Xor lrc        
        i = i + 1
    Loop
   
    getLRC = Chr$(lrc)
End Function

Funny what Google turns up... http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_20732543.html
GivenRandyCommented:
I think you should be starting with 00000000 and THEN xor'ing 00000001, right?
GivenRandyCommented:
Strike that, duh. Let me see why it's different.
freezingHotAuthor Commented:
i tried the one you have, Chaosian, and it returns an exclamation mark.  i tried it last night it couldn't get it to work.

GivenRandy:  I tried your function against another string and it was off by 20 again.
GivenRandyCommented:
Duh, because it's hex. More like this:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ' 01 30 02 42 20 21 20 03 The LRC result is 53
        Dim MyString As String
        MyString = Chr(&H1)
        MyString &= Chr(&H30)
        MyString &= Chr(&H2)
        MyString &= Chr(&H42)
        MyString &= Chr(&H20)
        MyString &= Chr(&H21)
        MyString &= Chr(&H20)
        MyString &= Chr(&H3)
        MessageBox.Show("Result is: " & CalculateLRC(MyString))
    End Sub

    Public Function CalculateLRC(ByVal S As String) As Byte
        CalculateLRC = 0
        For Index As Integer = 1 To Len(S)
            CalculateLRC = CByte(CalculateLRC Xor Asc(Mid(S, Index, 1)))
        Next
    End Function
GivenRandyCommented:
It returns 83, which is 53H. The CalculateLRC() function is unchanged. I just had the wrong input values going into it.
GivenRandyCommented:
If you wanted the hex conversion:

        MessageBox.Show("Result is: " & Hex(CalculateLRC(MyString)) & " hexadecimal")
GivenRandyCommented:
So the finished version:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ' 01 30 02 42 20 21 20 03 The LRC result is 53
        Dim MyString As String
        MyString = Chr(&H1)
        MyString &= Chr(&H30)
        MyString &= Chr(&H2)
        MyString &= Chr(&H42)
        MyString &= Chr(&H20)
        MyString &= Chr(&H21)
        MyString &= Chr(&H20)
        MyString &= Chr(&H3)
        MessageBox.Show("Result is: " & CalculateLRC(MyString) & " decimal")
        MessageBox.Show("Result is: " & Hex(CalculateLRC(MyString)) & " hexadecimal")
    End Sub

    Public Function CalculateLRC(ByVal S As String) As Byte
        CalculateLRC = 0
        For Index As Integer = 1 To Len(S)
            CalculateLRC = CByte(CalculateLRC Xor Asc(Mid(S, Index, 1)))
        Next
    End Function

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
freezingHotAuthor Commented:
fantastic.

the one item i added was

Return Hex(CalculateLRC) at the end of the calculateLRC function as i need to include this in the string i send....

i appreciate the quick response and clean code.

have a good one.

Jeff CertainCommented:
well, thgat one's returning a string. Wonder if ! is Chr(53)?
freezingHotAuthor Commented:
sometimes when the answer is obvious, it's overlooked, Chaosian.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.