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

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

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