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 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

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