vb.netencodingnetworkstream

VB.net Howto encode a String with different Encondings for Tcp Chat Client


i have an issue encoding strings im receiving with my tcp-chat-client. im pretty sure the string contains 2 different encodings and i can not figure out how to handle it the best way so that german umlaute (äüöß) showing correct in a richtextbox. i tried almost all encodings hence and forth but i can not get it to work.

the problem is that usernames and channel names using ISO-8859-15 or 1252 (both work) and the messages written by each users are using UTF8 or Default Encoding.

Würzburg#Mägermän#Da fallen mir mehrere GrÃŒnde ein. Als erstes, schnellstmöglich erben

Channel and Nickname encoding is okay but user message is not using the correct encoding. What would be the best way to solve a problem like this? Im Using Regex to split the string in single variables for Username/Channel/Message - Should i use Replace() to replace the Umlauts or is there a better way? Thank you for your ideas!


Solution

  • You can use this function, which converts the text with the ‘incorrect’ encoding to text with the correct encoding (for display in your Richtextbox). I have assumed ‘latin1’ (iso-8859-1) as the incorrect encoding.

        Sub Main()
            Dim text As String = "schnellstmöglich|Oberfläche"
            System.Diagnostics.Debug.WriteLine(ConvertEncoding(text))
        End Sub
    
        Private Function ConvertEncoding(text As String) As String
            If String.IsNullOrEmpty(text) Then Return String.Empty
            Dim supposedTextOnInterface As String = text
            Dim tcpEncoding As System.Text.Encoding = System.Text.Encoding.GetEncoding("iso-8859-1")
            Dim supposedCharsAsBytes As Byte() = tcpEncoding.GetBytes(supposedTextOnInterface)
            Dim utf8Text As String = System.Text.Encoding.UTF8.GetString(supposedCharsAsBytes)
            Return utf8Text
        End Function