Create Checksum

aaronk

Well-Known Member
Licensed User
Longtime User
Hello,

I am trying to convert a JavaScript checksum code into Basic4Andriod code can anyone help me out..

I have started / tried but can't get it to work..

JavaScript code:
B4X:
var cmd = "8300660A1234E" 
var sum = 0;
for (var i = 0; i< cmd.length; i++){

sum += cmd.charCodeAt(i);

}

var chk = ((sum ^ 0xFF ) + 1).toString(16);
if (chk.length > 2){
  chk = chk.substr(chk.length - 2,2);
}else if (chk.length < 2){
   while(chk.length < 2){
      chk = "0" + chk;
   }
}

return cmd + chk.toUpperCase() + String.fromCharCode(13) + String.fromCharCode(10);;


Basic4Android code:
B4X:
Dim sum As String 
   Dim chk As String
   sum = "743"
   chk = ((sum ^ "0xFF") + 1)
   If chk.Length > 2 Then
   chk = chk.SubString (1)
   Else
   If chk.Length < 2 Then
   While chk.Length < 2
   chk = "0" + chk
   End If
   
   ToastMessageShow(sum & chk,True)

Based on my example above the final outcome in JavaScript and Basic4Andriod should read:

sum & chk put together: 8300660A1234E49

49 being the checksum
 

stefanobusetto

Active Member
Licensed User
Longtime User
yust a few hints
far from solution

B4X:
Dim cmd As String 
cmd = "8300660A1234E" 

Dim sum As Int 
sum = 0

For i = 0 To cmd.Length -1 
    Dim c As Char
   c = cmd.CharAt(i)
   Log ( c )
   
   sum = sum + Asc(c)
Next

Log ( sum )



Dim chk As String
chk = ( Bit.Xor (sum , 255 ) + 1)

' 0xFF means hexadecimal FF 
' which is decimal 255

' the javascript ^ operator is xor

If chk.Length > 2 Then
   chk = chk.SubString (1)
Else
If chk.Length < 2 Then
   Do While chk.Length < 2
      chk = "0" + chk
   Loop
End If   
End If

Log(chk)
 
Last edited:
Upvote 0

aaronk

Well-Known Member
Licensed User
Longtime User
Hello,

I have got this working in vb6.0 but need help converting it to Basic4Andriod code.. anyone able to help?

VB6.0 Code:
B4X:
Public Function CalculateCRC(MyData As String) As String

   Dim a As String
   Dim Var2 As String
   Dim Var4 As String
   Dim Var5 As String
   a = Len(MyData)
   a = Hex(a + 2)

   If Len(a) < 2 Then
       Var2 = "0" & a & MyData
   Else
       Var2 = a & MyData
   End If

   a = Len(Var2)

   Dim b As Integer
   Dim num As Integer

   For b = 1 To a
       num = num + Asc(Mid(Var2, b, 1))
   Next b

   Var4 = num Mod 256
   Var5 = Hex(CByte(((Not Val(Var4)) + 1) And 255))

   While Len(Var5) < 2
       Var5 = "0" & Var5
   Wend

   CalculateCRC = Var2 & Var5

End Function
 
Upvote 0

aaronk

Well-Known Member
Licensed User
Longtime User
The JavaScript and VB6.0 both do the same thing at the end of the day.

Just need a way for at least one of them to work with Basic4Android.
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
Didn't test it thoroughly, yet you can give it a try:
B4X:
Sub CalculateCRC(MyData As String) As String
   Dim a As String   
   Dim Var2 As String
   Dim Var4 As String
   Dim Var5 As String
   'a = Len(MyData)
   a=MyData.Length 
   'a = Hex(a + 2)
   a=Bit.ToHexString(a+2)
   a=a.ToUpperCase 
   'If Len(a) < 2 Then
   If a.Length<2 Then
       Var2 = "0" & a & MyData
   Else
       Var2 = a & MyData
   End If
    'a = Len(Var2)
    a=Var2.Length
    'Dim b As Integer
    'Dim num As Integer
    Dim b As Int
    Dim num As Int 
    For b = 1 To a
       'num=num+asc(Mid(Var2, b, 1)))
       num = num + Asc(Var2.CharAt(b-1))
    'Next b
    Next
    Var4 = num Mod 256
   'Var5 = Hex(CByte(((Not Val(Var4)) + 1) AND 255))
    Dim tempByte As Byte
    tempByte=Bit.And ((Bit.Not(Var4)+1),255)
    Var5 = Bit.ToHexString (tempByte)
    Var5=Var5.SubString (Var5.Length -2)
    'While Len(Var5) < 2
   Do While Var5.Length  < 2
       Var5 = "0" & Var5
   'Wend
    Loop
   'CalculateCRC = Var2 & Var5
   Dim tempCRC As String 
   tempCRC=Var2 & Var5.Touppercase
   Return tempCRC
End Sub
You should call this sub for example as
B4X:
Dim temp As String 
temp=CalculateCRC("yourStringHere")
 
Last edited:
Upvote 0

aaronk

Well-Known Member
Licensed User
Longtime User
Thanks for your reply. Works just like I wanted it to.

however I have another checksum that I need help with and not sure how to do it in Basic4Andriod..

Lets say I have a string: 8300560A123E

you would then need to convert it to:

8 = 38
3 = 33
0 = 30
0 = 30
5 = 35
6 = 36
0 = 30
A = 41
1 = 31
2 = 32
3 = 33
E = 45

Then you need to add them up in HEX
38+33+30+30+35+36+30+41+31+32+33+45 = 282.

Then get the answer but only get the last 2 digits (82 in this example)

Then 100 - 82 = 7E (our checksum would be 7E)

anyone know how to do this in Basic4Andriod?
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User

Try this:
B4X:
Sub checkSum2
Dim tempString As String 
tempString="8300560A123E"
Dim sum1 As Long 
sum1=0
For k=0 To tempString.Length -1
sum1=sum1+Asc(tempString.CharAt(k))
Next
Dim sum2hex As String 
sum2hex=Bit.ToHexString (sum1)
sum2hex=sum2hex.SubString (sum2hex.Length -2)
Dim tempval As Long
tempval=256-Bit.ParseInt(sum2hex,16)
Dim finalVal As String 
finalVal=Bit.ToHexString(tempval)
finalVal=finalVal.ToUpperCase 
Msgbox(finalVal,"ok")
End Sub

After previous example, I would suggest following the code and doing any necessary changes for your other modules. Just a suggestion.
 
Upvote 0

stefanobusetto

Active Member
Licensed User
Longtime User
You can use mod 256 to get the last 2 digits
So you can work only with numbers
Converting to hex only at the end
 
Upvote 0

aaronk

Well-Known Member
Licensed User
Longtime User

Thanks Heaps.. works just like how I want it.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…