Android Question small function can not convert to B4A

Sinan Tuzcu

Well-Known Member
Licensed User
Longtime User
Hi,

I have a function or a sub in VB.NET but can not convert in B4A.
Can anyboady help

VB.NET
B4X:
  Private Function Send(ByRef Klartext As String) As Boolean
        Dim idx As Byte
        Dim tmpByte As Byte
        Dim tmpChar As String
        Dim Textsammeln As String = ""

        For idx = 1 To CByte(Len(Klartext.Trim))
            tmpChar = Mid(Klartext.Trim, idx, 1)
            tmpByte = CByte(Asc(tmpChar) + 3)
            tmpChar = Chr(tmpByte)
            Mid(Klartext, idx, 1) = tmpChar
            Textsammeln = Trim(Klartext)
        Next idx

        Return Senden(Textsammeln)
    End Function

B4A
This I have try it:
B4X:
For idx = 1 To Klartext.Trim.Length
    tmpChar = Klartext.SubString2(idx, 1).Trim
    tmpByte = Asc(tmpChar) + 3
    tmpChar = Chr(tmpByte)
    Klartext.SubString2(idx, 1) = tmpChar
    Textsammeln =  Textsammeln & tmpChar.Trim
Next
    Return Send(Textsammeln)
 End Sub


get compiler error regarding Substring2
 

OliverA

Expert
Licensed User
Longtime User
Try
B4X:
    Dim idx As Int
    Dim tmpByte As Byte
    Dim tmpChar As String
    
    Dim sb As StringBuilder
    sb.Initialize
    For idx = 1 To Klartext.Trim.Length
        tmpChar = Klartext.SubString2(idx, 1)
        tmpByte = Asc(tmpChar) + 3
        sb.Append(Chr(tmpByte))
    Next
    Return Send(sb.ToString)
BTW, reading up on CByte (https://www.oreilly.com/library/view/vbnet-language-in/0596003080/re32.html), your original code makes a lot of assumptions:
1) The length of Klartext is between 0 to 255 (inclusive) characters. If it is over 255 characters, CByte would bomb out
2) The unicode value returned by the first Mid call is between 0 to 252 (inclusive), otherwise, the next call to CByte is going to bomb out
Note: The above two points are only valid if the linked documentation is valid. I'm not a VB.Net user

As to the new code you tried:

B4X:
tmpChar = Klartext.SubString2(idx, 1).Trim
does not equal
B4X:
tmpChar = Mid(Klartext.Trim, idx, 1)
You are calling Trim AFTER extracting the substring in the new code. That could lead to a slight bug in cases where white spaces are located inside the string (not at the end)

Strings in B4X are immutable (https://www.javatpoint.com/immutable-string). That's why
B4X:
Klartext.SubString2(idx, 1) = tmpChar
does not work and why you should not build strings via & (since you are creating a new string with each &, not appending to an existing string), but via StringBuilder
 
Upvote 0

Geezer

Active Member
Licensed User
Longtime User
If i remember correctly, the substring and substring2 start with 0 ... so 0 to length - 1
 
Upvote 0

Sinan Tuzcu

Well-Known Member
Licensed User
Longtime User
Thanks,

but the code in VB.NET works very well
B4X:
 Private Function Send(ByRef Klartext As String) As Boolean
        Dim idx As Byte
        Dim tmpByte As Byte
        Dim tmpChar As String
        Dim Textsammeln As String = ""

        For idx = 1 To CByte(Len(Klartext.Trim))
            tmpChar = Mid(Klartext.Trim, idx, 1)
            tmpByte = CByte(Asc(tmpChar) - 4)
            tmpChar = Chr(tmpByte)
            Mid(Klartext, idx, 1) = tmpChar
            Textsammeln = Trim(Klartext)
        Next idx

        Return Senden(Textsammeln)
    End Function

and your posted code is not equal with this.
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Upvote 0

Sinan Tuzcu

Well-Known Member
Licensed User
Longtime User
Hi,

if I send this so then it works very well
B4X:
'"1|255|" & CRLF <-- each charecter are minus 4
Send(Chr(45) & Chr(120) & Chr(46) & Chr(49) & Chr(49) & Chr(120)  & CRLF)


private Sub Send(msg As String)
    Dim Buffer() As Byte
    msg = msg & CRLF
  
    Buffer = msg.GetBytes("Windows-1252") 

    AStreams.Write2(Buffer,0,Buffer.Length)
End Sub


but if I do this with this code, then not works
B4X:
SendComando("1|" & "255"  & "|" & CRLF)

Private Sub SendComando(Klartext As String)
     Dim idx As Int
    Dim tmpByte As Byte
    Dim tmpChar As String
  
    Dim sb As StringBuilder
    sb.Initialize
    For idx = 0 To Klartext.Trim.Length-1
        tmpChar = Klartext.trim.SubString(idx)
        tmpByte = Asc(tmpChar.Trim) - 4
        sb.Append(Chr(tmpByte))
    Next
    Return Send(sb.ToString.Trim)

End Sub

private Sub Send(msg As String)
    Dim Buffer() As Byte
    msg = msg & CRLF
  
    Buffer = msg.GetBytes("Windows-1252") 

    AStreams.Write2(Buffer,0,Buffer.Length)
End Sub
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Send(Chr(45) & Chr(120) & Chr(46) & Chr(49) & Chr(49) & Chr(120) & CRLF)
The above <> the below
SendComando("1|" & "255" & "|" & CRLF)
In the first quoted code, you did not change the CRLF values
But in the second quoted code, the SendComando will change the CRLF (by subtracting 4 from CR and 4 from LF)
Note: Other issues may exist, but this is the first one that stand out without looking at the code
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
B4X:
Private Sub SendComando(Klartext As String)
    Dim idx As Int
    Dim tmpByte As Byte
    Dim tmpChar As String
  
    Dim sb As StringBuilder
    sb.Initialize
    For idx = 0 To Klartext.Trim.Length-1
        tmpChar = Klartext.trim.SubString(idx)
        tmpByte = Asc(tmpChar.Trim) - 4
        Log(tmpByte)
        sb.Append(Chr(tmpByte))
    Next
    Send(sb.ToString.Trim)

End Sub

private Sub Send(msg As String)
    Dim Buffer() As Byte
    msg = msg & CRLF
  
    Buffer = msg.GetBytes("Windows-1252")
    Log("--Buffer--")
    For x = 0 To Buffer.Length - 1
        Log(Buffer(x))
    Next
End Sub
Using the above code and calling it with
B4X:
SendComando("1|" &  "255" & "|")
Gives me
45
120
46
49
49
120
--Buffer--
45
120
46
49
49
120
10
So to me it looks like the conversion is correct. The only thing that may not produce what you expect is the CRLF. In my case, it only produces an LF. If you always want to add CR and LF to your message, then use
B4X:
private Sub Send(msg As String)
    Dim Buffer() As Byte
    msg = $"${msg}${Chr(13)}${Chr(10)}"$ 'Start using Smart String Literals: https://www.b4x.com/android/forum/threads/b4x-smart-string-literal.50135/
  
    Buffer = msg.GetBytes("Windows-1252")
    Log("--Buffer--")
    For x = 0 To Buffer.Length - 1
        Log(Buffer(x))
    Next
End Sub
And then the output changes to
45
120
46
49
49
120
--Buffer--
45
120
46
49
49
120
13
10
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…