-1

I have application wrote in C#, and this application receives data through network from server using sockets (udp libenet).

In my application I have function to process raw bytes sent in packet. One of functions is reading string, delimited by \0.

My problem is that I'm sending UTF-8 encoded string by server to C# application, but when I use these strings to display them in controls, I get gibberish instead of polish letters.

Function that reads strings from buffer:

public override string ReadString()
{
            
    StringBuilder sb = new StringBuilder();

    while (true)
    {
        byte b;
        if (Remaining > 0)
            b = ReadByte();
        else
            b = 0;

        if (b == 0) break;

        // Probably here is the problem. Checked other encodings etc., but still same 
        sb.Append(Encoding.UTF8.GetString(new byte[] { b }, 0, 1));
    }

    return sb.ToString();
}

Function overrides, the one from:

public class BitReader : BinaryReader

In my application I get:

Polish letters turned into gibberish in dataviewgrid

Community
  • 1
  • 1
user3157855
  • 714
  • 2
  • 9
  • 24
  • Reasonably copy-paste ready code is available in http://stackoverflow.com/questions/30523775/c-server-and-java-client-tcp-socket-communication-issues. – Alexei Levenkov Jan 05 '16 at 21:13

1 Answers1

3

You can't read UTF-8 byte wise as a single character might take more than one byte.

See How to convert byte[] to string? (first read everything into one byte array / List).

Community
  • 1
  • 1
Matthias
  • 12,053
  • 4
  • 49
  • 91
  • Thank you, works :) Correct solution I'm using is: pushing all bytes into List, then converting it using Encoding.UTF8.GetString(list.ToArray()) – user3157855 Jan 05 '16 at 21:30