Android Question CRC16A problrem

Theera

Well-Known Member
Licensed User
Longtime User
I would like the experts to help me about the code ,I have VB6.0 code is
B4X:
Function CRC16A(buffer() As Byte) As Long

Dim i As Long
Dim Temp As Long
Dim crc As Long
Dim j As Integer
    crc = &HFFFF&
    For i = LBound(buffer) To UBound(buffer)
        Temp = buffer(i) * &H100&
        crc = crc Xor Temp
           
           For j = 0 To 7
                If (crc And &H8000&) Then
                    crc = ((crc * 2) Xor &H1021&) And &HFFFF&
                Else
                    crc = (crc * 2) And &HFFFF&
                End If
            Next j
Next i
CRC16A = crc And &HFFFF
    
End Function
and
B4X:
...
...
Dim m As String
m = IIf(Val(Text5.Text) = 0, QRCodePromptPayPart1 & QRCodePromptPayPart3, QRCodePromptPayPart1 & QRCodePromptPayPart2 & QRCodePromptPayPart3)
Dim aBuf() As Byte
    
    Dim crc As Long
    
    aBuf = StrConv(m, vbFromUnicode)

    
     crc = CRC16A(aBuf)
    
    
    QRCodePromptPayIs = m & Hex(crc)
...
...
I would like to use in B4A ,I don't know how to code these line as belows
B4X:
    aBuf = StrConv(m, vbFromUnicode)
and
B4X:
  QRCodePromptPayIs = m & Hex(crc)

I have coded function CRC() is belows
B4X:
Private Sub CRC16A(Buffer() As Byte) As Long

Dim i As Long
Dim Temp As Long
Dim crc As Long
Dim j As Int
    crc = 0xHFFFF
    'For i = LBound(Buffer) To UBound(Buffer)
    For i = 0 To Buffer.Length - 1
        Temp = Buffer(i) * 0xH100
        'crc = crc Bit.Xor Temp
        crc = Bit.Xor (crc ,Temp)
           
           For j = 0 To 7
                'If (crc Bit.And 0xH8000) Then
            If  Bit.And(crc, 0xH8000) Then
                   ' crc = ((crc * 2) Bit.Xor 0xH1021) And 0xHFFFF
                crc = Bit.And( Bit.Xor((crc * 2), 0xH1021), 0xHFFFF)
                Else
                    'crc = (crc * 2) Bit.And 0xHFFFF
                crc =  Bit.And( (crc * 2), 0xHFFFF)
                End If
           Next 
   Next 


    Return  Bit.And( crc,0xHFFFF)

End Sub
 

Theera

Well-Known Member
Licensed User
Longtime User
There are several threads on CRC
I have read it.But.I don't know how tocode these 2 lines.(strconv(), hex())
B4X:
aBuf = StrConv(m, vbFromUnicode)
And
B4X:
QRCodePromptPayIs = m & Hex(crc)
 
Last edited:
Upvote 0

Theera

Well-Known Member
Licensed User
Longtime User
The lastest ,I have the code
B4X:
    Dim m As String=sf2.iif(sf2.Val(Label10.Text)=0,cs.Append(content1).Append(content3),cs.Append(content1).Append(content2).Append(content3))
    Dim aBuf() As Byte
    'Dim crc As Long
    Dim crc As Int
    Dim ByteConv As ByteConverter
    'aBuf=ByteConv.StringToBytes(m,"UTF8")
    aBuf=m.GetBytes("UTF8")
    crc=CRC16A(aBuf)
    Log(crc)
    Dim tmp2 As String=crc
    Dim tmp3 As String=Bit.ToHexString(ByteConv.HexFromBytes(tmp2.GetBytes("UTF8")))
     cs= m & tmp3

B4X:
  cs= m & tmp3
,But I can't code,it has error "Types do not match" ( cs as StringBuilder)
 
Upvote 0

Theera

Well-Known Member
Licensed User
Longtime User
Okay, It's my mistake. I must use .append() instead of it.
 
Upvote 0

Theera

Well-Known Member
Licensed User
Longtime User
I have eror code
An error has occurred in sub:chkinguest_crc16a(java line:749) java.lang.RuntimeException: Cannot parse:32768 as boolean continue?
my code is belows
B4X:
Private Sub CRC16A(Buffer() As Byte) As Long
    Dim Temp As Long
    Dim crc1 As Long
    crc1 = 0xFFFF
    
    
    'For i = LBound(Buffer) To UBound(Buffer)
    For i = 0 To Buffer.Length - 1
        
        Temp = Buffer(i) * 0x0100
        
        crc1 = Bit.Xor (crc1 ,Temp)
           
        For j = 0 To 7
            
            If  Bit.And(crc1, 0x8000) Then
                
                crc1 = Bit.And( Bit.Xor((crc1 * 2), 0x1021), 0xFFFF)
            Else
                
                crc1 =  Bit.And( (crc1 * 2), 0xFFFF)
            End If
        Next
    Next
    
    crc1=Bit.And( crc1,0xFFFF)
    Return  crc1
End Sub

I can't solve it ,please help me.
 
Upvote 0

Theera

Well-Known Member
Licensed User
Longtime User
where is your sample project?
My project is about promptpay.I can'tsend all of the code. But.It used crc16A() function for checksum. I have tried to compare between Erel's code and mine.
 
Upvote 0

Theera

Well-Known Member
Licensed User
Longtime User
I have test Erel's crc() instead of mine, program can run, but.my crc16A() can't.
 
Upvote 0

Theera

Well-Known Member
Licensed User
Longtime User
My.original.source code vb6.0.is post#1
 
Upvote 0

emexes

Expert
Licensed User
I feel things will go better for you with this too:

B4X:
    For i = 0 To Buffer.Length - 1
        '''Temp = Buffer(i) * 0x0100
        Temp = Bit.And(Buffer(i), 0xFF) * 0x0100    'convert B4X signed Byte -128..127 to unsigned 0..255

and tbh that multiplication should really be a left-shift but on the other hand it ain't broke so let's leave it be ? for now ?
 
Upvote 0

MicroDrie

Well-Known Member
Licensed User
Longtime User
It would be super-useful if you had sample CRC16A sample input bytes and corresponding output long, to confirm that your ported B4X function is generating the same results as the original VB6 function.
Be aware that the CRC16 calculation takes place over the byte value that applies to an ASCII character. The original ASCII character has an 8 bit value in one byte. In Windows you may have to deal with an extended two-byte charter code for a character code. Without a known test string and the expected result, a CRC16 calculation says nothing about the correctness of the calculation.
 
Upvote 0

emexes

Expert
Licensed User
Be aware that the CRC16 calculation takes place over the byte value that applies to an ASCII character. The original ASCII character has an 8 bit value in one byte. In Windows you may have to deal with an extended two-byte charter code for a character code. Without a known test string and the expected result, a CRC16 calculation says nothing about the correctness of the calculation.

I do not understand what this has to do with my posts about getting the CRC16A routine working.

But I cheerfully agree I had not considered that the VB6 CRC16A code's Bytes would be any size other than 8-bits.
 
Last edited:
Upvote 0

Theera

Well-Known Member
Licensed User
Longtime User
After I have test.My poblem is rather to the line, I will try to code.
B4X:
If  Bit.And(crc1, 0x8000) Then
 
Upvote 0

Theera

Well-Known Member
Licensed User
Longtime User
This is my the small code.
 

Attachments

  • TestPromptPay.zip
    13.5 KB · Views: 150
Upvote 0

Theera

Well-Known Member
Licensed User
Longtime User
In VB6.0 is
B4X:
'Must cut comma  all from the cost at cashmoney
QRCodePromptPayPart2 = Trim("54") & Format$(str(Len(StrCutCommaAll(cashmoney))), "00") & Trim(StrCutCommaAll(cashmoney))

In B4A is (I'm not sure that it 's correct?)
B4X:
    Dim tmp As String=sf2.Len(NumberFormat2(cashmoney,1,0,0,False))
    'Log(tmp)
    Dim content2 As String=sf2.Trim("54")&NumberFormat(tmp,2,0)
    'Log(content2)
 
Upvote 0

Similar Threads

Top