Android Byte Array Checksum checkSum2(byte[] data)

Here you can find the source of checkSum2(byte[] data)

Description

check Sum

Declaration

public static int checkSum2(byte[] data) 

Method Source Code

//package com.java2s;

public class Main {
    public static int checkSum2(byte[] data) {
        long checksum = 0;
        int length = data.length;

        byte[] temp = new byte[2];
        for (int index = 0; index < length - 1; index += 2) {
            temp[0] = data[index];/*from  www .jav  a 2s  .  c  o  m*/
            temp[1] = data[index + 1];

            int val = bytes2short(temp) & 0x0000FFFF;
            checksum += val;
        }

        return (int) (checksum & 0xFFFFFFFF);
    }

    public static short bytes2short(byte[] data) {
        short val = 0;

        val |= data[0] & 0x00FF;
        val |= data[1] << 8;

        return val;
    }
}

Related

  1. checkSum(byte[] data)
  2. checkSum(byte[] bytes)
  3. checkSum(byte[] cmdPackage)