Other [solved] C Code to B4X

DonManfred

Expert
Licensed User
Longtime User
I don´t know C.

Can anyone help me translating this C-Code to B4X?

B4X:
int main()
#define POLYCRC32 0xEDB88320u

unsigned int PolyCrc32(const unsigned char *buff, int len)
{
    unsigned int crc=0;
    int i,j;
  
    for (i=0;i<len;i++) {
        crc^=buff[i];
        for (j=0;j<8;j++) {
            if (crc&1) crc=(crc>>1)^POLYCRC32; else crc>>=1;
        }
    }
    return crc;
}

char *btmac = "84:EB:18:35:F0:B3";
int len = 17;
int crc = PolyCrc32(btmac,len);
 
Solution
using in-line java (tested in B4J)
B4X:
Sub AppStart (Args() As String)
    Dim theBytes() As Byte = "84:EB:18:35:F0:B3".GetBytes("ascii")
    Log("crc : " & PolyCrc32(theBytes))
End Sub
Public Sub PolyCrc32(buff() As Byte) As Long
    Dim jo As JavaObject=Me
    Return jo.RunMethod("PolyWobble",Array(buff))
End Sub

#if Java
public static int PolyWobble(byte[] bytes){
        int crc  = 0;       // initial contents of LFBSR
        int poly = 0xEDB88320;   // reverse polynomial

        for (byte b : bytes) {
            int temp = (crc ^ b) & 0xff;

            // read 8 bits one at a time
            for (int i = 0; i < 8; i++) {
                if ((temp & 1) == 1) temp = (temp >>> 1) ^ poly;
                else                 temp...

Star-Dust

Expert
Licensed User
Longtime User
I don't know C either, but it looks like that for the function.

B4X:
Public Sub PolyCrc32(buff As String) As Int
    Dim Crc As Byte = 0

    For i=0 To buff.Length-1
        Crc=Bit.Xor(Crc,Asc(buff.CharAt(i)))
        For J=0 To 7
            If Bit.And(Crc,1)>0 Then  Crc=Bit.Xor(Bit.ShiftRight(Crc,1),0xEDB88320)
            Else
                Crc=Bit.ShiftLeft(Crc,1)
            End If
        Next
    Next

    Return Crc
End Sub


B4X:
CRC=PolyCrc32("84:EB:18:35:F0:B3")
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
(Just in case, should apply)
If the C code that you posted works with unsigned values, the C shifRght operator (>>) will NOT bring a '1' to the most significant bit, in case the number to be shifted has its most significant bit set to '1'. Contrarily, it will if the numbers are declared as signed. May be wrong, but I think this is how it worked.
In B4A, the numbers we are using are signed (so it will set the MSb). It has to be taken into account to mimic the C behavior.

If the above is right, taking @Star-Dust code as a reference and applying it, I think it should be nearer to the final solution

(re-edited)
B4X:
Public Sub PolyCrc32(buff() as byte, len as int) As Int
    Dim Crc As Int = 0

    For i=0 To len-1
        Crc=Bit.Xor(Crc,buff(i))
        For J=0 To 7
            ' Bit.shiftRight will add a '1' bit to the highest position if the number already has it.
            If Bit.And(Crc,1)<>0 Then  Crc=Bit.Xor( Bit.And(Bit.ShiftRight(Crc,1), 0x7FFFFFFF) ,0xEDB88320)
            Else
                Crc=Bit.ShiftRight(Crc,1)
            End If
        Next
    Next

    Return crc
End Sub
 
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Unfortunately both solutions give the wrong result.

I do not have much info.
Only the above code and:
Oscar supports Bluetooth connection and needs simple version check after connected in order to avoid invalid connection.
 Command
VERSION <POLYCRC32 of BT MAC>
 Example
Use polycrc32 to get parameter -1184182848 when Oscar’s Bluetooth device MAC address is 84:EB:18:35:F0:B3.
Send below command to Oscar ending with CRLF.
VERSION -1184182848

The expected result (CRC) for the String "84:EB:18:35:F0:B3" is: -1184182848

@Star-Dust Code returns 0
@JordiCP Code returns 5262
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
The problem maybe is the ascii code. Verify that the string produces asci codes with number 0-255 otherwise the values are distorted.
For the rest I don't know I don't know C. Just a few notions
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
using in-line java (tested in B4J)
B4X:
Sub AppStart (Args() As String)
    Dim theBytes() As Byte = "84:EB:18:35:F0:B3".GetBytes("ascii")
    Log("crc : " & PolyCrc32(theBytes))
End Sub
Public Sub PolyCrc32(buff() As Byte) As Long
    Dim jo As JavaObject=Me
    Return jo.RunMethod("PolyWobble",Array(buff))
End Sub

#if Java
public static int PolyWobble(byte[] bytes){
        int crc  = 0;       // initial contents of LFBSR
        int poly = 0xEDB88320;   // reverse polynomial

        for (byte b : bytes) {
            int temp = (crc ^ b) & 0xff;

            // read 8 bits one at a time
            for (int i = 0; i < 8; i++) {
                if ((temp & 1) == 1) temp = (temp >>> 1) ^ poly;
                else                 temp = (temp >>> 1);
            }
            crc = (crc >>> 8) ^ temp;
        }
    return crc;
}    
#End If

Returns :
crc : -1184182848
 
Upvote 1
Solution

Star-Dust

Expert
Licensed User
Longtime User
There is an error in my code. I have translated a ShiftRight as ShiftLeft
B4X:
Public Sub PolyCrc32(buff As String) As Int
    Dim Crc As Byte = 0

    For i=0 To buff.Length-1
        Crc=Bit.Xor(Crc,Asc(buff.CharAt(i)))
        For J=0 To 7
            If Bit.And(Crc,1)>0 Then  Crc=Bit.Xor(Bit.ShiftRight(Crc,1),0xEDB88320)
            Else
                Crc=Bit.ShiftRight(Crc,1)
            End If
        Next
    Next

    Return Crc
End Sub
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Just a couple of updates to @Star-Dust's code
B4X:
Public Sub PolyCrc32(buff As String) As Int
Dim Crc As Int = 0

For i=0 To buff.Length-1
    Crc=Bit.Xor(Crc,Asc(buff.CharAt(i)))
    For J=0 To 7
    If Bit.And(Crc,1)>0 Then  
        Crc=Bit.Xor(Bit.UnsignedShiftRight(Crc,1),0xEDB88320)
    Else
    Crc=Bit.UnsignedShiftRight(Crc,1)
End If
Next
Next

Return Crc
End Sub
No in-line necessary. The UnsignedShiftRight method emulates C's >>
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
Use UnsignedShiftRight and make Crc an Int and you're all set...
I don't like the C language. I just wrote something down for friend Don who helps everyone. Otherwise not even for idea :p
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
I've got to own up - the inline java code wasn't written by me - a 1 minute google search found it.
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
Just a couple of updates to @Star-Dust's code
B4X:
Public Sub PolyCrc32(buff As String) As Int
Dim Crc As Int = 0

For i=0 To buff.Length-1
    Crc=Bit.Xor(Crc,Asc(buff.CharAt(i)))
    For J=0 To 7
    If Bit.And(Crc,1)>0 Then
        Crc=Bit.Xor(Bit.UnsignedShiftRight(Crc,1),0xEDB88320)
    Else
    Crc=Bit.UnsignedShiftRight(Crc,1)
End If
Next
Next

Return Crc
End Sub
No in-line necessary. The UnsignedShiftRight method emulates C's >>
This works too. This is B4X
B4X:
Log("Result: " & PolyCrc32("84:EB:18:35:F0:B3"))
Waiting for debugger to connect...
Program started.
Result: -1184182848
 
Upvote 0
Top