tcp read bytes

cardmaker

Member
Joined
Jan 12, 2019
Messages
6
Programming Experience
Beginner
hello, why i cant read all bytes ? i receive 27 bytes confirmed whit wireshark.

hex1.jpg


my code for print:

C#:
                Byte[] buffer = new Byte[27];
                // String to store the response ASCII representation.
                String responseData = String.Empty;

                // Read the first batch of the TcpServer response bytes.
                stream.Read(buffer, 0, buffer.Length);
                //string sss = (Convert.ToString(Convert.ToChar(buffer[9])));
                //MessageBox.Show(sss);
                responseData = System.Text.Encoding.Default.GetString(buffer, 0, buffer.Length);
                MessageBox.Show(responseData);
it only print hello from and not the bytes 80 , 19 , 22, 50, 12 ? anyone know why?
 

Sheepings

Senior Programmer
Joined
Sep 5, 2018
Messages
655
Location
UK
Programming Experience
10+
If the size of the data is more than your buffer can handle, you won't be receiving all the data. Check that first to rule it out.
 

Skydiver

Well-known member
Joined
Apr 6, 2019
Messages
646
Location
Virginia Beach, VA
Programming Experience
10+
What is the return value from stream.Read()? Did to that actually read 27 bytes?

What is System.Text.Encoding.Default on your system? It varies between different environments. See the documentation. Depending on the encoding, bytes will be decoded into the .NET Framework's internal Unicode representation differently.
 

Skydiver

Well-known member
Joined
Apr 6, 2019
Messages
646
Location
Virginia Beach, VA
Programming Experience
10+
On my machine that is setup for US-ENG as its default culture, here's the output I get:
Capture.png


0x19 is the ASCII control character EM (End Medium)
0x22 is the double quote
0x50 is the capital letter 'P'
0x12 is the ASCII control character DC2 (Device Control 2)

Notice that depending on the encoding the 0x80 was decoded to be 0x20AC, 0x003F, or 0xFFFD.

All ASCII codes less than 0x20 are technically non-printing characters, but some consoles may display something for them depending on the locale of the console.

When I run the following code:
C#:
using System;
using System.Text;
using System.Linq;

public class Test
{
    static void DecodeAndShowString(byte [] buffer, Encoding encoding)
    {
        var str = encoding.GetString(buffer, 0, buffer.Length);
        Console.WriteLine(encoding.EncodingName);
        Console.WriteLine($"{str.Length}: '{str}'");
        Console.WriteLine(String.Join(":", str.ToCharArray().Select(ch => $"{(int)ch:x4}")));
    }

    static void Main()
    {
        byte[] buffer =
        {
            0x01, 0x01, 0x68, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x66, 0x72, 0x6F, 0x6D, 0x20, 0x80, 0x19,
            0x22, 0x50, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
        };

        DecodeAndShowString(buffer, System.Text.Encoding.Default);
        DecodeAndShowString(buffer, System.Text.Encoding.ASCII);
        DecodeAndShowString(buffer, System.Text.Encoding.UTF8);
    }
}
 
Top Bottom