Calculate the LRC of the given byte array - CSharp System

CSharp examples for System:Byte Array

Description

Calculate the LRC of the given byte array

Demo Code

/*//from  w  w w. ja  v a  2 s  . c o  m
 * Copyright 2012 Mario Vernari (http://cetdevelop.codeplex.com/)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
using System;

public class Main{
        /// <summary>
        /// Calculate the LRC of the given byte array
        /// </summary>
        /// <param name="buffer">The source byte array</param>
        /// <param name="offset">The index of the first byte to be considered</param>
        /// <param name="count">The number of the bytes to be considered</param>
        /// <returns>The resulting value</returns>
        public static byte CalcLRC(
            byte[] buffer,
            int offset,
            int count)
        {
            int lrc = 0;

            for (int i = offset, upper = offset + count; i < upper; i++)
            {
                lrc ^= buffer[i];
            }

            return (byte)lrc;
        }
}

Related Tutorials