Android Question Converting a character to an integer

andrewtheart

Member
Licensed User
Longtime User
I must convert the following VB.NET code to Basic4Android code.

B4X:
 Dim iCheckSumTotal As Integer
        Dim sCheckSumTotal As String
        Dim sCheckSumResponse As String
        Dim i As Integer
        Dim iCommandLength As Integer
        iCommandLength = command.Length
        i = 0

        While i < iCommandLength
            iCheckSumTotal = iCheckSumTotal + Convert.ToInt32(Convert.ToChar(command.Substring(i, 1)))
            i += 1
        End While

I tried many things. This is what I came up with, and it generates an exception at runtime. I knew it wouldn't work, but after searching the forums I just don't see a way to do this.

B4X:
  Dim iCheckSumTotal As Int
        Dim sCheckSumTotal As String
        Dim sCheckSumResponse As String
        Dim i As Int
        Dim iCommandLength As Int
        iCommandLength = command.Length
        i = 0

        Do While i < iCommandLength
            Dim charatt As String = command.CharAt(i)
           
            iCheckSumTotal = iCheckSumTotal + charatt
            i = i+ 1
        Loop

Any assistance would be appreciated
 

derez

Expert
Licensed User
Longtime User
If the chars are all numbers then the int value of a char is this:
asc(charat(i)) - 30
 
Upvote 0

andrewtheart

Member
Licensed User
Longtime User
Thanks much.

The char can be a number or a letter. Do I need logic to differentiate between the two, and subtract a different number if it's a letter?


If the chars are all numbers then the int value of a char is this:
asc(charat(i)) - 30
 
Upvote 0

derez

Expert
Licensed User
Longtime User
Thanks KitCarlson, you are right, it is asc(charat(i) - 48 .
Andrewtheart -
The char can be a number or a letter.
These are the values in ascii table, if you think that the transformation of A,B,C to int as 17,18,19 is OK then you can use it for non-integer characters. I just don't know what is the meaning of the transformation to integers.
 
Last edited:
Upvote 0
Top