Java Checksum Calculate calcChecksum(byte[] buffer, int offset, int length)

Here you can find the source of calcChecksum(byte[] buffer, int offset, int length)

Description

Returns the checksum for the specified bytes.

License

Open Source License

Parameter

Parameter Description
buffer the buffer where the bytes are in.
offset the offset from where to start calculating the checksum.
length the length of bytes to calculate the checksum for.

Return

the checksum for the specified bytes.

Declaration


public static byte calcChecksum(byte[] buffer, int offset, int length) 

Method Source Code

//package com.java2s;
/**//from   w  ww  .j a  v  a  2s. c o m
 * Copyright (c) 2010-2015, openHAB.org and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 */

public class Main {
    /**
     * Returns the checksum for the specified bytes.
     * 
     * @param buffer
     *            the buffer where the bytes are in.
     * @param offset
     *            the offset from where to start calculating the checksum.
     * @param length
     *            the length of bytes to calculate the checksum for.
     * @return the checksum for the specified bytes.
     */

    public static byte calcChecksum(byte[] buffer, int offset, int length) {
        byte crc = 0x7F;

        for (int i = 0; i < length; i++) {
            crc = (byte) ((crc - buffer[offset + i]) & 0x7F);
        }

        return crc;
    }
}

Related

  1. calcChecksum(byte[] buffer, int start, int end)
  2. calcCheckSum(byte[] bytes, int num)
  3. calcCheckSum(byte[] data, int offset, int maxIdx)
  4. calcChecksum(String value, int length)