Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

VB to Java, WTF?

Name: Anonymous 2011-10-26 10:37

Hey /prog/

I'm trying to port a CRC16 function from VB to Java, and I cannot for the life of me see what I'm doing wrong.  Any ideas?

Here's the VB, and passing in [ 0x00 0x01 0x40 0x30 ] returns [ 0x18 0x2E ]:

    Function CRC16(ByVal data() As Byte) As Byte()
        Dim CRC16Lo As Byte, CRC16Hi As Byte
        Dim CL As Byte, CH As Byte
        Dim SaveHi As Byte, SaveLo As Byte
        Dim i As Integer
        Dim Flag As Integer
        CRC16Lo = &HFF
        CRC16Hi = &HFF
        CL = &H8
        CH = &H84
        For i = 0 To UBound(data)
            CRC16Lo = CRC16Lo Xor data(i)
            For Flag = 0 To 7
                SaveHi = CRC16Hi
                SaveLo = CRC16Lo
                CRC16Hi = CRC16Hi \ 2
                CRC16Lo = CRC16Lo \ 2
                If ((SaveHi And &H1) = &H1) Then
                    CRC16Lo = CRC16Lo Or &H80
                End If
                If ((SaveLo And &H1) = &H1) Then
                    CRC16Hi = CRC16Hi Xor CH
                    CRC16Lo = CRC16Lo Xor CL
                End If
            Next Flag
        Next i
        Dim ReturnData(1) As Byte
        ReturnData(0) = CRC16Hi
        ReturnData(1) = CRC16Lo
        CRC16 = ReturnData
    End Function

Here's the Java, and passing in the same bytes as above returns [ 0x63 0xF5 ]

    public static byte[] get(byte[] data) {
       
        byte lo = (byte)0xFF;
        byte hi = (byte)0xFF;
       
        byte cl = (byte)0x08;
        byte ch = (byte)0x84;
       
        byte sl = 0;
        byte sh = 0;
       
        for(byte b : data) {
           
            lo ^= b;
           
            for(int i=0; i<8; i++) {
               
                sh = hi;
                sl = lo;
               
                hi >>= 2;
                lo >>= 2;
               
                if((sh & 1) == 1) {
                    lo |= 0x80;
                }
               
                if((sl & 1) == 1) {
                    hi ^= ch;
                    lo ^= cl;
                }
               
            }
        }
       
        return new byte[] {
            hi,
            lo
        };
       
    }

I'm not a VB developer so it's probably my understanding of arithmetic there that's screwing me up, but if anybody can point out how I'm an idiot, I'll be forever grateful.

Name: Anonymous 2011-10-26 10:41

out

Name: Anonymous 2011-10-26 11:06

Don't you want >>>=?

Name: Anonymous 2011-10-26 11:26

>>3
Yeah, I realized that mistake but it's not my problem -- I'm getting the same result back (63:f5)

Thanks anyway

Name: Anonymous 2011-10-26 11:35

>>4
Oh, found the problem. You're doing <<= 2 instead of <<= 1. VB's backslash operator isn't a bitshift, it's integer division.

Name: 5 2011-10-26 11:36

Fuck, I meant >>=.

Name: Anonymous 2011-10-26 11:37

I figured it out, if anybody wants to know, it's because Java is fucktarded and forces signedness.  Once I wasted a bunch of memory by using integers so they didn't overflow it worked fine.  Here's the code which appears to work:

    public static byte[] get(byte[] data) {
       
        int lo = 0xFF;
        int hi = 0xFF;
       
        int cl = 0x08;
        int ch = 0x84;
       
        int sl = 0;
        int sh = 0;
       
        for(byte b : data) {
           
            lo ^= b;
           
            for(int i=0; i<8; i++) {
               
                sh = hi;
                sl = lo;
               
                hi /= 2;
                lo /= 2;
               
                if((sh & 1) == 1) {
                    lo |= 0x80;
                }
               
                if((sl & 1) == 1) {
                    hi ^= ch;
                    lo ^= cl;
                }
               
            }
        }
       
        return new byte[] {
            (byte)(hi & 0xFF),
            (byte)(lo & 0xFF)
        };
       
    }

Name: Anonymous 2011-10-26 11:41

>>7
You changed from bitshifting to division.

Name: Anonymous 2011-10-26 11:42

>>8
Yeah, I did.  I was barking up the wrong tree on that.  As >>5 said, \ is integer division, not bit shifting.

Name: Anonymous 2011-10-26 12:01

OP here

This: 
lo ^= b;
Should be:
lo ^= b & 0xFF;

And then it appears to work for any input

Name: Anonymous 2011-10-26 12:10

porting CRC16
Just Google "CRC16 java" and you have a working implementation.

Name: Anonymous 2011-10-26 12:17

>>11
http://en.wikipedia.org/wiki/Cyclic_redundancy_check

>CRC-16-IBM    x16 + x15 + x2 + 1
>CRC-16-CCITT    x16 + x12 + x5 + 1
>CRC-16-T10-DIF    x16 + x15 + x11 + x9 + x8 + x7 + x5 + x4 + x2 + x + 1
>CRC-16-DNP    x16 + x13 + x12 + x11 + x10 + x8 + x6 + x5 + x2 + 1
>CRC-16-DECT    x16 + x10 + x8 + x7 + x3 + 1
>CRC-16-Fletcher    Not a CRC; see Fletcher's checksum

This is none of those

Name: FrozenVoid 2011-10-26 12:30

CRC is lame. use builtin SHA-1/MD-5 hashes which are part of Java.
http://www.javablogging.com/sha1-and-md5-checksums-in-java/

Name: Anonymous 2011-10-26 12:47

>>12
http://en.wikipedia.org/wiki/Computation_of_CRC
It should be trivial to figure out the polynomial from the code, and then modify existing code to use it.

Name: Anonymous 2011-10-26 15:55

Java
GC

Found your problem!

Name: Anonymous 2011-10-26 15:55

Also,

GC is shit.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List