Android Question Replace � as ""

petr4ppc

Active Member
Licensed User
Dear friends, please help,

I have string where I have this: 123x456�������������������������������������

How to replace please � as "" (nothing)

Best regards
p4ppc
 

petr4ppc

Active Member
Licensed User
Dear Erel,

this string comes if I receive UDP
B4X:
    Dim msg2 As String
    msg2 = BytesToString(Result,0, Result.Length,"UTF8")
msg2=123x456�������������������������������������...............
Lenght of string is 8192

Best regards
p4ppc
 

petr4ppc

Active Member
Licensed User
Yes Erel, here from service:
B4X:
Dim UDPSocket1 As UDPSocket

End Sub

Sub Service_Create
 UDPSocket1.Initialize("UDP", 5000, 8192)
 Dim server2 As ServerSocket
End Sub

Sub Service_Start (StartingIntent As Intent)

End Sub

Sub Service_Destroy

End Sub

Sub UDP_PacketArrived (Packet As UDPPacket)
        Dim msgudp As String
    msgudp=""
    msgudp = BytesToString(Packet.data,0, 4,"UTF8")
    CallSubDelayed2(Main, "receiveudp", Packet.data)
End Sub
 

Erel

Administrator
Staff member
Licensed User
B4X:
  Dim msgudp As String
    msgudp=""
    msgudp = BytesToString(Packet.data,0, 4,"UTF8")
Equivalent to:
B4X:
Dim msgudp As String = BytesToString(Packet.data,0, 4,"UTF8")
The length will be maximum 4 characters.

The correct code assuming that it is a text message encoded with UTF8:
B4X:
Dim msgudp As String = BytesToString(Packet.Data, Packet.Offset, Packet.Length, "UTF8")
 

petr4ppc

Active Member
Licensed User
Thank you erel and it is possible to do correction with delete � in the string? How can I do it? Please..
 

DonManfred

Expert
Licensed User
Last edited:

petr4ppc

Active Member
Licensed User
DonManfred - thank you for your answer, I go to read byteconverter library

best regards
p4ppc
 

emexes

Well-Known Member
Licensed User
I have string where I have this: 123x456�������������������������������������

How to replace please � as "" (nothing)

Best regards
p4ppc
Assuming that it is indeed a fixed-length string padded with NULs (ie ASCII 0x00), then

B4X:
Dim msg2 As String
msg2 = BytesToString(Result, 0, Result.Length, "UTF8").Replace(Chr(0), "")

If that doesn't work, then perhaps the �s are some other character (0x7F aka DEL is another common non-printable padding), so check using:

B4X:
Dim msg2 As String
msg2 = BytesToString(Result, 0, Result.Length, "UTF8")
Log("Last character is Chr(" & Asc(msg2.CharAt(msg2.Length - 1)) & ")")
Log("Last byte is " & Result(Result.Length - 1))
I would expect the same number in both those Logs, but... NUL is sometimes handled differently to other characters, particularly eg as an end-of-string marker, so I wouldn't like to stake my life on that expectation ;-)
 
Last edited:

emexes

Well-Known Member
Licensed User
Just in case you miss that subtle addition at first glance, that's:

msg2 = BytesToString(Result, 0, Result.Length,"UTF8").Replace(Chr(0), "")
 

petr4ppc

Active Member
Licensed User
Thank you very much friends, for your help and examples,

I have tried before sleeping Erels solution and its absolute 100 percent OK.

Best regards
p4ppc
 

emexes

Well-Known Member
Licensed User
it's absolute 100 percent OK
Absolute 100 percent?!? Nothing warms my heart more than a bit of unbridled optimism.

But if things do happen to go awry, and you find that msgupd.Length is unexpectedly shorter than Packet.Length... remember that bytes with the high bit set usually represent a multibyte UTF-8 character, eg 2 bytes = 1 character.

But if you're just sending plain 7-bit ASCII, or actual UTF-8... then perhaps 100% is correct.

:)
 
Top