Android Question Write ASCII characters

Brian Dean

Active Member
Licensed 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.
 

emexes

Well-Known Member
Licensed User
I need to write a thing like this: "Try to write chr"
View attachment 82985
If we step back a level so that we can see the forest for the trees... what are you actually trying to do, like: what is the end aim?

There is no problem doing precisely what you have asked for, but given that post #2 answered post #1, I suspect that post #3 is also not asking the right question.
 

emexes

Well-Known Member
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:

techknight

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