Try
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:
tmpChar = Klartext.SubString2(idx, 1).Trim
does not equal
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
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