iOS Question ToHexString Issue

aaronk

Well-Known Member
Licensed User
Longtime User
Hi,

The following B4A code works fine in B4A but when I copy it and use it in B4i it fails.

B4X:
Sub cmd1(value As String) As String
Try
Log(value)
Dim A As String
Dim Var2 As String
Dim Var4 As String
Dim Var5 As String
A=value.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 & value
Else
Var2 = A & value
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
Catch
Log(LastException.Message)
End Try
End Sub

The problem is with ToHexString

I then did a search and found: https://www.b4x.com/android/forum/threads/tohexstring.48215/#content

I then tried changing the code to be like the following:

B4X:
Sub cmd1(value As String) As String
Try
    Log(value)
   Dim A As String  
   Dim Var2 As String
   Dim Var4 As String
   Dim Var5 As String
   Dim bc As ByteConverter
   A=value.Length
    'a = Hex(a + 2)
   'A=Bit.ToHexString(A+2)
   A = bc.HexFromBytes(Array As Byte(Bit.ParseInt(A+2, 2)))
   A=A.ToUpperCase
    'If Len(a) < 2 Then
   If A.Length<2 Then
       Var2 = "0" & A & value
   Else
       Var2 = A & value
   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 = bc.HexFromBytes(Array As Byte(Bit.ParseInt(tempByte, 2)))
    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
Catch
Log(LastException.Description)
End Try   
End Sub

However when I compile the app it comes up with:
B4i version: 2.31
Parsing code. (0.00s)
Compiling code. (0.21s)
Compiling layouts code. (0.00s)
Compiling debugger engine code. Error
B4i line: 64
For b = 1 To A
javac 1.8.0_60
shell\src\xxx\xxxxxx\test\b4i_main_subs_0.java:129: error: > expected
final int limit36 = BA.ObjectToNumber(_a.<NSString*>get()).intValue;
^
1 error

I guess I am doing something wrong but can't work out how to fix this error..

Any ideas on how to fix this error?
 

aaronk

Well-Known Member
Licensed User
Longtime User
Now I have it as:

B4X:
Sub cmd1(value As String) As String
Try
    Log(value)
   Dim A As Int 
   Dim Var2 As String
   Dim Var4 As String
   Dim Var5 As String
   Dim bc As ByteConverter

   A = value.Length
    'a = Hex(a + 2)
   'A=Bit.ToHexString(A+2)
   A = bc.HexFromBytes(Array As Byte(Bit.ParseInt(A+2, 2)))
    'If Len(a) < 2 Then
  
    Var2 = A & value
    '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 = bc.HexFromBytes(Array As Byte(Bit.ParseInt(tempByte, 2)))
    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
Catch
Log(LastException.Description)
End Try  
End Sub

But it's returning the wrong thing.

If I do:
B4X:
Log(cmd1("as00"))
It should Log: 06as0066

Instead it's logging: 0as0001

(My original B4A code (first lot of code from post 1) is working fine though)
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
wouldn't this work in your original source?

Var5 = "0" & Bit.ToHexString (tempByte)
Var5=Var5.SubString (Var5.Length -2)

and drop the loop behind it.
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
you mean mine?

it adds a leading 0 so he would get 0b for "b" or 0bb for "bb" and his line below would strip it back to his needed 2 char based value (0b or bb).

if tohex works right it would just strip off the extra 0 so the code is good for B4A & B4i.
 
Upvote 0

aaronk

Well-Known Member
Licensed User
Longtime User
No. I meant the original code.
It will create a checksum based on the value.

2 ASCII characters, 2 digit checksum.
This is the hexadecimal two complement of the modulo-256 sum of the ASCII values of all characters in the message excluding the checksum itself. Permissible characters are ASCII 0-9 and Upper case A-F. When all characters are added to the checksum, the value should equal 0.

I have it working for B4A, but just can't get it to work for B4i (or B4J).
 
Upvote 0

aaronk

Well-Known Member
Licensed User
Longtime User
Can you post an example of a string and the expected value?

Example 1:
as00
It should become: 06as0066

Example 2:
ps00
It should become: 06ps0057

Example 3:
cr0100
It should become: 08cr010002


The following code works in B4A and if I can get it to work in B4i then I will be happy.

B4X:
Sub cmd1(value As String) As String
Try
Log(value)
Dim A As String
Dim Var2 As String
Dim Var4 As String
Dim Var5 As String
A=value.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 & value
Else
Var2 = A & value
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
Catch
Log(LastException.Message)
End Try
End Sub
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
This will work on all platforms:
B4X:
Sub cmd1(value As String) As String
   Dim bc As ByteConverter
   Dim sb As StringBuilder
   sb.Initialize
   sb.Append(bc.HexFromBytes(Array As Byte(value.Length + 2)))
   sb.Append(value)
   Dim tmp As String = sb.ToString
   Dim num As Int
   For i = 0 To tmp.Length - 1
     num = num + Asc(tmp.CharAt(i))
   Next
   num = num mod 256
   Dim tempByte As Byte = Bit.And(Bit.Not(num) + 1, 0xff)
   sb.Append(bc.HexFromBytes(Array As Byte(tempByte)))
   Return sb.ToString
End Sub
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
this might work aswell without the need of extra libraries

B4X:
Sub cmd1(value As String) As String
Dim A,Var2,Var4,Var5 As String
Dim b,num As Int
A="0" & Bit.ToHexString(value.Length+2).ToUpperCase
A=A.SubString(A.Length-2)
Var2=a & value
A=Var2.Length
For b = 1 To A
num = num + Asc(Var2.CharAt(b-1))
Next
Var4 = num Mod 256
Var5="0" & Bit.ToHexString (256-Var4)
Var5=Var5.SubString(Var5.Length-2)
Return Var2 & Var5.Touppercase
End Sub
 
Upvote 0

aaronk

Well-Known Member
Licensed User
Longtime User
Sub cmd1(value As String) As String
Dim A,Var2,Var4,Var5 As String
Dim b,num As Int
A=
"0" & Bit.ToHexString(value.Length+2).ToUpperCase
A=A.SubString(A.Length-
2)
Var2=a & value
A=Var2.Length

For b = 1 To A
num = num +
Asc(Var2.CharAt(b-1))
Next
Var4 = num Mod 256
Var5="0" & Bit.ToHexString (256-Var4)
Var5=Var5.SubString(Var5.Length-
2)
Return Var2 & Var5.Touppercase
End Sub
That doesn't work as 'ToHexString' is an unknown member. (B4i doesn't support ToHexString)

Erel has given me the best solution so I will stick to it for now. Thanks for your support though.
 
Upvote 0
Top