diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2015-07-07 15:20:48 +1200 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2015-07-07 15:20:48 +1200 |
commit | 6eb1837b796f51bd2a84d091b706a7ae1d4952a0 (patch) | |
tree | 859f37cc0db189c7564d45be888fe7758b9b02b1 /src/org/traccar/helper/Crc.java | |
parent | fa4215999ef38879ac72540c44e46ca39858a20b (diff) | |
download | trackermap-server-6eb1837b796f51bd2a84d091b706a7ae1d4952a0.tar.gz trackermap-server-6eb1837b796f51bd2a84d091b706a7ae1d4952a0.tar.bz2 trackermap-server-6eb1837b796f51bd2a84d091b706a7ae1d4952a0.zip |
Improve Aplicom id parsing
Diffstat (limited to 'src/org/traccar/helper/Crc.java')
-rw-r--r-- | src/org/traccar/helper/Crc.java | 30 |
1 files changed, 17 insertions, 13 deletions
diff --git a/src/org/traccar/helper/Crc.java b/src/org/traccar/helper/Crc.java index 213d1192a..418ec3132 100644 --- a/src/org/traccar/helper/Crc.java +++ b/src/org/traccar/helper/Crc.java @@ -154,21 +154,25 @@ public class Crc { return String.format("*%02x", checksum).toUpperCase(); } - public static String luhnChecksum(String s) { - int sum = 0; - boolean evenPosition = true; - for (int i = s.length() - 1; i >= 0; i--) { - int n = Integer.parseInt(s.substring(i, i + 1)); - if (evenPosition) { - n *= 2; - if (n > 9) { - n = (n % 10) + 1; + public static long luhnChecksum(long imei) { + long checksum = 0; + long remain = imei; + + for (int i = 0; remain != 0; i++) { + long digit = remain % 10; + + if ((i % 2) == 0) { + digit *= 2; + if (digit >= 10) { + digit = 1 + (digit % 10); } } - sum += n; - evenPosition = !evenPosition; + + checksum += digit; + remain /= 10; } - return String.valueOf(10 - sum % 10); - } + + return 10 - (checksum % 10); + } } |