Android Question Write ASCII characters

Marco Maria Vilucchi

Active Member
Licensed User
Longtime User
Thanks,
I need to write a thing like this: "Try to write chr"

upload_2019-8-9_12-6-20.png


How can I do it?
 
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
I'm not sure what you are asking for. Are you trying to split integers into ASCII characters?

B4X:
' Convert a 32-bit integer to four ASCII characters
Sub writeChars(value As Int)
    Dim i, k As Int
    For i = 0 To 3
        k = Bit.And(value, 0xFF)
        Log(Chr(k))
        value = Bit.ShiftRight(value, 8)
    Next   
End Sub

Somehow that seems too simple.
 
Upvote 0

emexes

Expert
Licensed User
Here's a reasonable interpretation - this code:
B4X:
Dim Translation As String = MarcoStrikesAgain("Try to write chr")
Log(Translation)

'pads a string with spaces at start, to length L, returns first L characters
Sub LeftPad(S As String, L As Int) As String
 
    Do While S.Length < L
        S = " " & S
    Loop
 
    Return S.SubString2(0, L)
 
End Sub

'translates string to match example layout structure
Sub MarcoStrikesAgain(StringToTranslate As String) As String
 
    Dim Index As Int = 0
    Dim NumOnLine As Int = 0
 
    Dim OutputString As String
 
    For I = 0 To StringToTranslate.Length - 1
        If NumOnLine = 0 Then
            If Index = 0 Then
                OutputString = OutputString & "Index     Bit 31-24  Bit 23-16  Bit 15-8   Bit 7-0" & CRLF
            End If
            Index = Index + 1
            OutputString = OutputString & LeftPad(Index, 5)
        End If
     
        OutputString = OutputString & LeftPad(StringToTranslate.CharAt(I), 11)
        NumOnLine = NumOnLine + 1
     
        If NumOnLine >= 4 Then
            OutputString = OutputString & CRLF
            NumOnLine = 0
        End If
    Next

    If NumOnLine > 0 Then
        OutputString = OutputString & CRLF
        NumOnLine = 0
    End If
 
    Return OutputString 

End Sub
produces this result:
B4X:
Index     Bit 31-24  Bit 23-16  Bit 15-8   Bit 7-0
    1          T          r          y         
    2          t          o                     w
    3          r          i          t          e
    4                     c          h          r
 
Last edited:
Upvote 0

techknight

Well-Known Member
Licensed User
Longtime User
When I see these kind of posts, I immediately gravitate towards someone trying to communicate with custom hardware/custom protocols.
 
Upvote 0
Top