Encodig.UTF8 problem on Chipworkx

I’m using this code for an UDP server

using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using System.Net;
using System.Net.Sockets;
using GHIElectronics.NETMF.Net;
using System.Text;

namespace MP3Shield
{
    class SendIPToClient
    {
        private const int port = 2000; 
        public static void SendIP()
        {
            using (Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
            {
                EndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, port);
                serverSocket.Bind(remoteEndPoint);
                if (serverSocket.Poll(-1, SelectMode.SelectRead))
                {
                    byte[] inBuffer = new byte[serverSocket.Available];
                    int count = serverSocket.ReceiveFrom(inBuffer, ref remoteEndPoint);
                    string message = new string(Encoding.UTF8.GetChars(inBuffer));
                    Debug.Print("Received: " + message);
                }
            }
        }
    }
}

Everything works and inBuffer receives the right bytes, which are “1,2,3,4,1,2,3,4,1,2,3,4”. But when I create message it contains little squares, like when you can’t visualize japanese kanji. What could it be?
Thanks

Show us the code that does not work :slight_smile:

How is the other end sending?

#################################################

Pop in the TinyCLR community IRC channel and ask/chat

about whatever you want.

Details here: http://www.tinyclr.com/forum/topic?id=7210

#################################################

This is the part that’s not working

string message = new string(Encoding.UTF8.GetChars(inBuffer));

This is the code of the client. It is written in full Framework and runs on PC

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;

namespace SendUdp
{
    class SendUdp
    {
        static void Main(string[] args)
        {
            if (args.Length<2)
            {
                string[] temp = new string[] {"255.255.255.255","2000"};
                args = temp;
            }
            
            if (args.Length != 2 && args.Length != 3)
                throw new ArgumentException("Parameter(s): <Destination> <Port> [<encoding>]");
            String server = args[0];
            int destPort = Int32.Parse(args[1]);

            UdpClient client = new UdpClient();

            byte[] messaggio = new byte[] { 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4 };

            client.Send(messaggio, messaggio.Length, server, destPort);
            client.Close();
        }
    }
}

@ GG - The bytes you are trying to convert to characters are not printable UTF-8 characters. The printable UTF-8 characters are in the range of 32-127 for single byte characters after which the characters require multiple bytes. All bytes below 32 are control characters. Try sending the following bytes

65, 66, 67, 68, 69

This should give you ABCDE.

1 Like
string messageS = "1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4";

byte[] buffer = Encoding.UTF8.GetBytes(messageS);

I managed to do it without using Encoding.UTF8 at all. I modified the server in this way. Basically I manually convert the bytes received to ASCII

namespace MP3Shield
{
    class SendIPToClient
    {
        private const int port = 2000; //scelta arbitraria
        public static void SendIP()
        {
            Debug.Print("Prova di ricezione del broadcast");
            using (Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
            {
                EndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, port);
                serverSocket.Bind(remoteEndPoint);
                if (serverSocket.Poll(-1, SelectMode.SelectRead))
                {
                    byte[] inBuffer = new byte[serverSocket.Available];
                    int count = serverSocket.ReceiveFrom(inBuffer, ref remoteEndPoint);
                    char[] message = new char[inBuffer.Length];
                    for (int n = 0; n < inBuffer.Length; n++)
                        message[n] = (char) (inBuffer[n] + 48); //conversion to ASCII
                    string s = new string(message);
                    Debug.Print(s);
                }
            }
        }
    }
}

What you did works fine.

But the Encoding.UTF8 method may execute faster, and is easier for someone who reads your code to understand what you are doing.

I’ll keep that in mind, thanks.
Consider that for this application I only need the actual byte array, which will contain even the IP address of the client who sent the broadcast message. I wanted to convert it to a string just for fun and debugging purposes. Infact, converting bytes like 192 to ASCII gives an undesired result.

Using this approach is shifting the bytes into the printable ASCII range, however, this will only work if the bytes you are sending are limited to 0-9. Higher numbers are going to show other characters or push you outside of the printable ASCII range( > 127) and create invalid UTF-8 code points.

Of course II do not know your use case I do not know if that is acceptable in your case, but I thought it might be worth mentioning.

I know it only works for a limited range, thanks.
As I said, for this application I only need the simple byte array. But since the Encoding wasn’t working, I wanted to know how to fix this thing