Announcement

Collapse
No announcement yet.

Calculate an LRC ?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Calculate an LRC ?

    I have to calculate an LRC (Longitudinal Redundcy Character).
    I have to figure out how to convert some code written in C++

    This code in C++:
    For (i=1;i<=1001; i++)
    lrc ^= buffer[i];

    If anyone can show me how to convert this to PowerBasic for DOS
    I would really be greatful. Several people have said that the
    ^= in C++ means XOR. I have tried to use XOR but with no success.

    Thanks,

    Johnny

    [This message has been edited by John Johnson (edited December 13, 2003).]

  • #2
    ^ means XOR but ^= means var = var XOR arg

    So:

    Code:
    For i = 1 to 1000
       lrc = lrc XOR buffer(i)
    Next

    ------------------
    Scott Slater
    Summit Computer Networks, Inc.
    www.summitcn.com
    Scott Slater
    Summit Computer Networks, Inc.
    www.summitcn.com

    Comment


    • #3
      Originally posted by Scott Slater:
      ^ means XOR but ^= means var = var XOR arg

      So:

      Code:
      For i = 1 to 1000
         lrc = lrc XOR buffer(i)
      Next

      Thanks Scott for your reply. It seems to work just fine. I
      just didn't understand the C++ meaning.

      Thanks again,
      Johnny


      ------------------

      Comment


      • #4
        ...I believe that should be
        Code:
        FOR i = 1 TO 1001
        ------------------
        Tom Hanlin
        PowerBASIC Staff

        Comment


        • #5
          Originally posted by Tom Hanlin:
          ...I believe that should be
          Code:
          FOR i = 1 TO 1001
          You're right Tom, I did allow for that. I just figured it was a typo.
          Thanks for being so alert.

          Johnny



          ------------------

          Comment


          • #6
            What type of VAR is Buffer(). The following code gives me a compile error. Does not like buffer() as String.

            Code:
            '   LRC Longitudinal Redundancy Check. A longitudinal redundancy check is a form
            '   of redundancy check that is applied independently to each of a parallel group of
            '   bit streams. The data must be divided into transmission blocks, to which the
            '   additional check data is added.
            '   Calculation:
            '   Set LRC = 0
            '   For each character c in the string
            '   Do
            '   Set LRC = LRC XOR c
            '   End Do
            '   The check begins after the STX and proceeds and includes the ETX.
            
            Function LRC(ByVal EdStr As String) As String
            Local l As String
            Local StartPosition As Long 
            Local j As Long 
            Local buffer() As String
                Dim buffer(Len(EdStr)) 
                l = "0"
                StartPosition = InStr(EdStr,$STX)
                StartPosition = StartPosition + 1
                For j = StartPosition to Len(EdStr)
                   buffer(j) = Mid$(EdStr,j,1)
                   LRC = LRC XOR buffer(i)
                Next
                LRC = l
            End Function

            Comment


            • #7
              Which compiler error does it give?
              Code:
              LRC = LRC XOR buffer(i)
              Where is the variable 'i' declared?
              Rod
              I want not 'not', not Knot, not Knott, not Nott, not knot, not naught, not nought, but aught.

              Comment


              • #8
                Compiler is PBwin 10.04, here is the final code that compiles. Not sure it returns the intended LRC value. Had to make function return a BYTE?

                Code:
                '   LRC Longitudinal Redundancy Check. A longitudinal redundancy check is a form
                '   of redundancy check that is applied independently to each of a parallel group of
                '   bit streams. The data must be divided into transmission blocks, to which the
                '   additional check data is added.
                '   Calculation:
                '   Set LRC = 0
                '   For each character c in the string
                '   Do
                '   Set LRC = LRC XOR c
                '   End Do
                '   The check begins after the STX and proceeds and includes the ETX.
                
                Function LRC(ByVal EdStr As String) As Byte
                Dim StartPosition As Local Long 
                Dim bLRC As Local Byte
                Dim bArr(Len(EdStr)) As Local Byte
                Dim i As Local Long
                
                'Calculate the LRC
                  For i = 0 To UBound(bArr())
                    bArr(i) = Val(Mid$(EdStr,i,1))
                    bLRC = bLRC Xor bArr(i)
                  Next i    
                  Function = bLRC
                End Function

                Comment


                • #9
                  > Not sure it returns the intended LRC value.

                  The only way you will know that is by performing the calculations manually and comparing. Pretty much the way you would test any code you create.

                  >Had to make function return a BYTE?

                  "As it turns out" the PB compiler handles conversions between numeric types automatically and transparently. Had you wanted the function to return, say, a LONG you would just have to use "AS LONG" in the procedure header and PB would have effected a conversion for you when you assigned bLRC to the FUNCTION.

                  The downside of this automatic data type conversion is that neither truncation, loss of precision (signficant digits) or rounding are considered either 'errors' or "warnings' when it is done.

                  I don't know about LRC but the XOR operator may be used on any integer-class variable (BYTE, INTEGER, LONG or QUAD). Since you defined "bLRC" as a byte only eight bits will be returned... and in context since you are always using a byte (VAL(MID$(stringavar, any, 1) as the other operand; eightbits XOR eightbits will return only eight bits of significance.

                  As someone around here is fond of saying, "Computers don't do the math. People do the math. Computers do the arithmetic."

                  MCM
                  Michael Mattias
                  Tal Systems Inc. (retired)
                  Racine WI USA
                  [email protected]
                  http://www.talsystems.com

                  Comment


                  • #10
                    IMO the line of code in post #8 that reads -
                    bArr(i) = Val(Mid$(EdStr,i,1))

                    should be -
                    bArr(i) = ASC(EdStr, i)

                    Just noticed another -
                    Dim bArr(Len(EdStr)) As Local Byte

                    should be -
                    Dim bArr(1 to Len(EdStr)) As Local Byte

                    The way you did it the array had one more element than length of string because the first element is index zero to length of string AUTOMATICALLY, Use the TO to specify the lower bound. Arrays that start at zero are said to be faster to access, but you must know about it and adjust the code to handle it.

                    Cheers,
                    Last edited by Dale Yarker; 29 Aug 2016, 05:24 PM.
                    Dale

                    Comment


                    • #11
                      I made the suggested changes and changed the function type to Integer.. The only description for how the LRC calculation is suppose to be done was provided in the documentation for the the LRC field. The docs have some examples of before and after the string is base 64 encoded. My LRC is returning a different value causing the transaction to return bad request. Here is the attached trace.

                      Code:
                      with out LRC XMLStr=<A001.28> Length= 10
                      With XMLStr=<A001.28I> Length= 11
                      Open bstrURL=http://10.1.1.61:10009?AkEwMBwxLjI4A0k=
                      Send Verb=GET bstrURL=<http://10.1.1.61:10009?AkEwMBwxLjI4A0k=> XML=<A001.28I>
                      Response=<<> 400<>Bad Request>
                      Here is the LRC calculation.

                      Code:
                      '   LRC Longitudinal Redundancy Check. A longitudinal redundancy check is a form
                      '   of redundancy check that is applied independently to each of a parallel group of
                      '   bit streams. The data must be divided into transmission blocks, to which the
                      '   additional check data is added.
                      '   Calculation:
                      '   Set LRC = 0
                      '   For each character c in the string
                      '   Do
                      '   Set LRC = LRC XOR c
                      '   End Do
                      '   The check begins after the STX and proceeds and includes the ETX.
                      
                      Function LRC(ByVal EdStr As String) As Integer
                      Dim StartPosition As Local Long 
                      Dim bLRC As Local Byte
                      Dim bArr(1 To Len(EdStr)) As Local Byte
                      Dim i As Local Long
                      
                      'Calculate the LRC
                        For i = 1 To UBound(bArr())
                          bArr(i) = Asc(EdStr,i)
                          bLRC = bLRC Xor bArr(i)
                        Next i    
                        Function = bLRC
                      End Function
                      The program appends the LRC result to the end of the string using .

                      XMLStr = XMLStr + Chr$(LRC(XMLStr))

                      The docs show the resulting string should end with a "K" as the LRC value, my LRC Function returns an "I", not sure why

                      Code:
                      3.3.2. Samples for Web Browser HTTP(S) Calls
                      Assume the destination of Terminal “IP: Port” is “192.168.2.100:10009”.
                      [02] as STX, [03] as ETX, [1c] as field separator.
                      3.3.2.1. Initialize (A00) (A01)
                      http://192.168.2.100:10009?AkEwMBwxLjI4A0s=
                      Message before base64 encoded: http://192.168.2.100:10009?[02]A00[1c]1.28[03]K
                      Trying to detemine how to fix the PB version of the LRC function to return an "K" instead of an "L"?







                      Comment


                      • #12
                        Turns out the LRC calculation was supposed to start with the second charater of the passed in string. This resulting code worked. The STX is the first character in the passed in string.

                        Appreciate everyones contribution.


                        Code:
                        '   LRC Longitudinal Redundancy Check. A longitudinal redundancy check is a form
                        '   of redundancy check that is applied independently to each of a parallel group of
                        '   bit streams. The data must be divided into transmission blocks, to which the
                        '   additional check data is added.
                        '   Calculation:
                        '   Set LRC = 0
                        '   For each character c in the string
                        '   Do
                        '   Set LRC = LRC XOR c
                        '   End Do
                        '   The check begins after the STX and proceeds and includes the ETX.
                        
                        Function LRC(ByVal EdStr As String) As Integer
                        Dim StartPosition As Local Long 
                        Dim bLRC As Local Integer
                        Dim bArr(1 To Len(EdStr)) As Local Integer
                        Dim i As Local Long
                        bLRC = 0
                        'Calculate the LRC
                          For i = 2 To UBound(bArr())
                            bArr(i) = Asc(EdStr,i)
                            bLRC = bLRC Xor bArr(i)
                        '    Call PTTrace("i="+Str$(I)+" bArr(i)="+Str$(bArr(i))+" bLRC="+Str$(bLRC))
                          Next i    
                        '  Call PTTrace("bLRC="+Str$(bLRC))
                          Function = bLRC
                        End Function

                        Comment


                        • #13
                          ???????
                          Why change the function return type to integer?
                          Dale

                          Comment

                          Working...
                          X