Java Checksum Calculate calcChecksum(String value, int length)

Here you can find the source of calcChecksum(String value, int length)

Description

calc Checksum

License

Apache License

Declaration

public static char calcChecksum(String value, int length) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    public static char calcChecksum(String value, int length) {
        if (value != null && value.length() == length + 1) {
            value = value.substring(0, length);
        }//ww w .  j a v  a 2s .  c o m
        if (value == null || value.length() != length) {
            throw new IllegalArgumentException(
                    "Illegal size of value; must be either" + length + " or " + (length + 1) + " characters");
        }
        int oddsum = 0;
        int evensum = 0;
        for (int i = value.length() - 1; i >= 0; i--) {
            if ((value.length() - i) % 2 == 0) {
                evensum += Character.digit(value.charAt(i), 10);
            } else {
                oddsum += Character.digit(value.charAt(i), 10);
            }
        }
        int check = 10 - ((evensum + 3 * oddsum) % 10);
        if (check >= 10)
            check = 0;
        return Character.forDigit(check, 10);
    }
}

Related

  1. calcChecksum(byte[] buffer, int offset, int length)
  2. calcChecksum(byte[] buffer, int start, int end)
  3. calcCheckSum(byte[] bytes, int num)
  4. calcCheckSum(byte[] data, int offset, int maxIdx)
  5. calculateChecksum(InputStream is, String algorithm)
  6. calculateChecksums(Optional zos, InputStream inputStream, Set checksumAlgorithms)
  7. checkSum(boolean[] a)
  8. checkSum(byte abyte0[])