Android Question convert code C to B4A

BluSky76

Member
Licensed User
Longtime User
Hello guys,
I have to convert this code to B4A.
----------------------------------------------------------------------------
B4X:
ushort Calc_CRC (uchar *Data, short Size)
{
ushort r=0, a, b;
short t;
for (t=0; t<Size; t++)
{
r+=(t*Data[t])+t;
}
a=r>>8; if (a==0x0D) a=0x0E;
b=r&0xFF; if (b==0x0D) b=0x0E;
a<<=8;
return a|b;
}
------------------------------------------------------------------------------
Thanks
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User
please use CODE Tags when posting code
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
I used the C language for 22 minutes and then abandoned it.
But I think it must be translated this way. The most expert will be able to correct.

B4X:
Sub Calc_CRC (Data () As Byte) As Int
    'ushort r=0, a, b;
    'short t;
    Dim r = 0, a, b As Int
   
    For t=0 To Data.Length-1
        'r+=(t*Data[t])+t;
        r = r + (t * Data(t))+t
    Next
   
    'a=r>>8; if (a==0x0D) a=0x0E;
    a=Bit.ShiftRight(r,8): If (a=0x0D) Then a=0x0e
    'b=r&0xFF; if (b==0x0D) b=0x0E;
    b=Bit.And(r,0xFF): If (b=0x0d) Then b=0x0e
    'a<<=8;
    a=Bit.ShiftLeft(a,8)
   
    'return a|b;
    Return Bit.Or(a,b)
End Sub
 
Last edited:
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
looks good, the only difference is now unsigned and signed
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
unsigned and signed
Of course, but I do not think we can do this in B4X and maybe it's not necessary based on the algorithm.

If we understand what we need, we might understand better.


If I try to translate the algorithm and not the code ... it could be translated as well:
B4X:
Sub calculate_crc(Data () As Byte) As Long
    'ushort r=0, a, b;
    'short t;
    Dim r As Long = 0
    Dim a, b As Int
 
    For t=0 To Data.Length-1
        'r+=(t*Data[t])+t;
        r = r + (t * Data(t))+t
    Next
 
    'a=r>>8; if (a==0x0D) a=0x0E;
    a=r/256: If (a=13) Then a=14
    'b=r&0xFF; if (b==0x0D) b=0x0E;
    b=r Mod 256: If (b=13) Then b=14

    'a<<=8;
    'return a|b;
    Return ((a * 256)+b)
End Sub
 
Last edited:
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
i mean this Data(t) was before uchar 0-255 and now a byte -128 to 127
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
i mean this part, i guess it give different result. btw i saw that b4a understand UByte but i do not know which lib it is.
r = r + (t * 0)+t
r = r + (t * -128)+t
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
I'm sure that it can be done with B4X.
I meant that I do not believe it was possible to declare UChar or UShort in B4X because they do not exist at the moment.

i mean this Data(t) was before uchar 0-255 and now a byte -128 to 127
This is easy to solve, just state Char. and then translate into value with Ascii ... see example

B4X:
Sub calculate_crc(Data () As Char) As Long
    'ushort r=0, a, b;
    'short t;
    Dim r As Long = 0
    Dim a, b As Int

    For t=0 To Data.Length-1
        'r+=(t*Data[t])+t;
        r = r + (t * Asc(Data(t)))+t
    Next

    'a=r>>8; if (a==0x0D) a=0x0E;
    a=r/256: If (a=13) Then a=14
    'b=r&0xFF; if (b==0x0D) b=0x0E;
    b=r Mod 256: If (b=13) Then b=14
 
    'a<<=8;
    'return a|b;
    Return ((a * 256)+b)
End Sub

also can write:

B4X:
Sub calculate_crc(Data () As Byte) As Long
    'ushort r=0, a, b;
    'short t;
    Dim r As Long = 0
    Dim Ap as long
    Dim a, b As Int

    For t=0 To Data.Length-1
        'r+=(t*Data[t])+t;
        AP = Data(t): If Ap <0 then Ap=Ap+256
        r = r + (t * Ap)+t
    Next

    'a=r>>8; if (a==0x0D) a=0x0E;
    a=r/256: If (a=13) Then a=14
    'b=r&0xFF; if (b==0x0D) b=0x0E;
    b=r Mod 256: If (b=13) Then b=14

    'a<<=8;
    'return a|b;
    Return ((a * 256)+b)
End Sub

For every problem there is a solution. Do not despair
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I meant that I do not believe it was possible to declare UChar or UShort in B4X because they do not exist at the moment.
That's true.

This line from your first post:
B4X:
r = r + (t * Data(t))+t

Should be:
B4X:
r = r + (t * Bit.And(0xFF, Data(t)))+t

Other than that it looks correct.
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
To not know the C language, I did not do too much bad shape then. :p

Thanks Erel
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
I do not know. The translation seems correct, as I told you, not development in C.
Erel who also knows the C language has suggested only a change to the code.

Which of the 3 did you use?
The byte Array from where you get it. Are you sure it's not a string?

Maybe if you tell us this CRC what it refers to (containers, packages, shipping, tax codes) maybe we can find the original algorithm on the web.
 
Upvote 0
Top