Home T-SQL checksum() in Java
Reply: 0

T-SQL checksum() in Java

fpk
1#
fpk Published in 2018-02-13 08:35:12Z

I am trying to port the T-SQL checksum to java, with some code I found using Google.

Our scanned documents contains a code, and this code needs validation. That is, in T-SQL checksum first 6 characters, and compute and validate the 7th character.

Example: The code is S5WKBCK, S5WKBC gives a checksum 158510959, and modulo 27 gives 7, and 7 is the correct 7th character in the string BCDFGHJKMNPQRSTWXYZ23456789.

Code

    int count = 0;
    long a = 0L;
    long b = 0L;
    long c = 0;
    long overflow = 0L;

    if (str.length() != 7) {
        System.out.println("- Failed code proof length test: " + str + " -> length: " + str.length());
        return false;
        }

    while (count < str.length() -1) {

        a = c << 4;
        b = str.getBytes()[count];
        c = a ^ b;
        overflow = c >> 32;
        c = c & 0xFFFFFFFF;
        c = c ^ overflow;

        System.out.printf( "a=%15d %45s\n", a, Long.toBinaryString( a ));
        System.out.printf( "b=%15d %45s\n", b, Long.toBinaryString( b ));
        System.out.printf( "c=%15d %45s\n", c, Long.toBinaryString( c ));
        System.out.println( "---------------------------------------------------------------");
        count++;
    }

    if (c > 2147483647) {
        c = c - max;
    } else if (c >= 32768 && c <= 65535) {
        c = c - 65536;
    } else if (c >= 128 && c <= 255) {
        c = c - 256;
    }

    String strCharacters = "BCDFGHJKMNPQRSTWXYZ23456789";
//  Java result
//        int intModulus = toIntExact(c) % 27;
    int intModulus = (int)c % 27;
    String strCalculatedChar = strCharacters.substring(intModulus, intModulus + 1); 
    System.out.printf("Java checksum  : %s, modulo %d=%d, %s, %s\n", c, 27, toIntExact(c) % 27, strCharacters, strCalculatedChar);

//  T-SQl result      
    intModulus = 158510959 % 27;
    strCalculatedChar = strCharacters.substring(intModulus, intModulus + 1); 
    System.out.printf("T-SQL checksum : %s, modulo %d=%d, %s, %s\n", "158510959", 27, toIntExact(158510959) % 27, strCharacters, strCalculatedChar);
//        
    String strLastChar = str.substring(6, 7); 
    if (strLastChar.equals(strCalculatedChar)) {
       System.out.println("- Passed code proof");
       return true;
    } else {
       System.out.println("- Failed code proof: " + str + ", calculated character: " + strCalculatedChar); 
       return false;
//           return true;
    }

With this code I got the following output

File: S5WKBCK_testcodeS5WKBCK.pdf
a=              0                                             0
b=             83                                       1010011
c=             83                                       1010011
---------------------------------------------------------------
a=           1328                                   10100110000
b=             53                                        110101
c=           1285                                   10100000101
---------------------------------------------------------------
a=          20560                               101000001010000
b=             87                                       1010111
c=          20487                               101000000000111
---------------------------------------------------------------
a=         327792                           1010000000001110000
b=             75                                       1001011
c=         327739                           1010000000000111011
---------------------------------------------------------------
a=        5243824                       10100000000001110110000
b=             66                                       1000010
c=        5243890                       10100000000001111110010
---------------------------------------------------------------
a=       83902240                   101000000000011111100100000
b=             67                                       1000011
c=       83902307                   101000000000011111101100011
---------------------------------------------------------------
Java binary_checksum()  : 83902307, modulo 27=23, BCDFGHJKMNPQRSTWXYZ23456789, 6
- Failed code proof: S5WKBCK, calculated character: 6

T-SQL checksum() : 158510959, modulo 27=7, BCDFGHJKMNPQRSTWXYZ23456789, K
- Passed code proof: S5WKBCK, calculated character: K

So, I got a working java binary_checksum() instead of a checksum().

Please some advice, Thanks in advance, Kind regards.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.454348 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO