B4J Question CRC -CCITT vb.net function conversion to b4j

Michael Sergio

Member
Licensed User
Longtime User
Hi.
I would like some help with the conversion from this function from vb.net :

B4X:
    Public Function ComputeCRC(val As Byte()) As Byte()

        Dim crc As Long
        Dim q As Long
        Dim c As Byte
        crc = 0
        For i As Integer = 0 To val.Length - 1
            c = val(i)
            q = (crc Xor c) And &HF
            crc = (crc >> 4) Xor (q * &H1081)
            q = (crc Xor (c >> 4)) And &HF
            crc = (crc >> 4) Xor (q * &H1081)
        Next
        Return New Byte() {CByte(crc And &HFF), CByte(crc >> 8)}

    End Function

I need this to calculate the checksum for my data that i`m sending to the SerialPort.
I tried making a conversion, but I don`t know even how to begin.
Any help would be appreciated.
Thank you.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
It is better to post an example of input and the expected output so we can test it.

Try this:
B4X:
Sub ComputeCRC (val() As Byte) As Byte()
   Dim crc As Long
   Dim q As Long
   Dim c As Byte
   For i = 0 To val.Length - 1
     c = val(i)
     q = Bit.And(Bit.Xor(crc, c), 0xF)
     crc = Bit.Xor(Bit.ShiftRight(crc, 4), q * 0x1081)
     q = Bit.And(Bit.Xor(crc, Bit.ShiftRight(c, 4)), 0xF)
     crc = Bit.Xor(Bit.ShiftRight(crc, 4), q * 0x1081)
   Next
   Return Array As Byte(Bit.And(crc, 0xFF), Bit.ShiftRight(crc, 8))
End Sub
You can use ByteConverter.HexFromBytes to see the bytes arrays values.
 
Upvote 0

Michael Sergio

Member
Licensed User
Longtime User
It is better to post an example of input and the expected output so we can test it.

Try this:
B4X:
Sub ComputeCRC (val() As Byte) As Byte()
   Dim crc As Long
   Dim q As Long
   Dim c As Byte
   For i = 0 To val.Length - 1
     c = val(i)
     q = Bit.And(Bit.Xor(crc, c), 0xF)
     crc = Bit.Xor(Bit.ShiftRight(crc, 4), q * 0x1081)
     q = Bit.And(Bit.Xor(crc, Bit.ShiftRight(c, 4)), 0xF)
     crc = Bit.Xor(Bit.ShiftRight(crc, 4), q * 0x1081)
   Next
   Return Array As Byte(Bit.And(crc, 0xFF), Bit.ShiftRight(crc, 8))
End Sub
You can use ByteConverter.HexFromBytes to see the bytes arrays values.

Yes, this is perfect , it is EXACTLY what i need.
B4X:
Dim FstLine As String="FC0511" 
    Dim Secline As String= bc.HexFromBytes(ComputeCRC(bc.HexToBytes(FstLine)))
    Log(FstLine & Secline)
The result that I expected was : "FC05112756" , and the conversion is working, i have the same result in B4j.

Thank you very much for your help and for this awesome IDE .I`m still learning, but i made some basic programs that i use on my Raspberry Pi , including the usage of the GPIO`s.
Best wishes.
 
Upvote 0

sales

Member
Licensed User
Longtime User
It is better to post an example of input and the expected output so we can test it.

Try this:
B4X:
Sub ComputeCRC (val() As Byte) As Byte()
   Dim crc As Long
   Dim q As Long
   Dim c As Byte
   For i = 0 To val.Length - 1
     c = val(i)
     q = Bit.And(Bit.Xor(crc, c), 0xF)
     crc = Bit.Xor(Bit.ShiftRight(crc, 4), q * 0x1081)
     q = Bit.And(Bit.Xor(crc, Bit.ShiftRight(c, 4)), 0xF)
     crc = Bit.Xor(Bit.ShiftRight(crc, 4), q * 0x1081)
   Next
   Return Array As Byte(Bit.And(crc, 0xFF), Bit.ShiftRight(crc, 8))
End Sub
You can use ByteConverter.HexFromBytes to see the bytes arrays values.

that code will generate CRC-CCITT (Kermit) from: FC0511 CRC: 2756

then i need to get CRC-CCITT (XModem) from: FC0511 expected CRC: 6BD6

https://www.lammertbies.nl/comm/info/crc-calculation.html


which code should be modified?

many thanks
 
Last edited:
Upvote 0
Top