Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static boolean isQRISValid(String qrdata) {
- //checsum 4 digit ascii
- if (qrdata != null && qrdata.length() > 4) {
- String qrDataNonCRC = qrdata.substring(0, qrdata.length() - 4);
- String qrCRC = qrdata.substring(qrdata.length()-4).toUpperCase();
- if (qrDataNonCRC.startsWith("00") && qrCRC.equals(checkCRC(qrDataNonCRC.getBytes()))) {
- return true;
- }
- }
- return false;
- }
- public static String checkCRC(byte []bytes) {
- int crc = 0xFFFF;
- int polynomial = 0x1021;
- String sCRC = "";
- for (byte b : bytes) {
- for (int i = 0; i < 8; i++) {
- boolean bit = ((b >> (7-i) & 1) == 1);
- boolean c15 = ((crc >> 15 & 1) == 1);
- crc <<= 1;
- if (c15 ^ bit) crc ^= polynomial;
- }
- }
- crc &= 0xffff;
- sCRC = Integer.toHexString(crc).toUpperCase();
- System.out.println("CRC16-CCITT = " + sCRC);
- return sCRC;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement