# 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?

LVL 1
###### 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.

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

The hex values are in a string which are received from a COM port.
Commented:
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
Author Commented:
hmmmm...

00000001 (01)
00110000 (30)
00000010 (02)
01000010 (42)
00100000 (20)
00100001 (21)
00100000 (20)
00000011 (03)
-----------
01010011 = 53
Commented:
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
Commented:
I think you should be starting with 00000000 and THEN xor'ing 00000001, right?
Commented:
Strike that, duh. Let me see why it's different.
Author 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.
Commented:
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
Commented:
It returns 83, which is 53H. The CalculateLRC() function is unchanged. I just had the wrong input values going into it.
Commented:
If you wanted the hex conversion:

MessageBox.Show("Result is: " & Hex(CalculateLRC(MyString)) & " hexadecimal")
Commented:
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

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

Author 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.

Commented:
well, thgat one's returning a string. Wonder if ! is Chr(53)?
Author 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.