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:
Basic4Android code:
Based on my example above the final outcome in JavaScript and Basic4Andriod should read:
sum & chk put together: 8300660A1234E49
49 being the checksum
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