TCPSocket send as UTF-8?

Is there a way to force data sent via the TCPSocket to be sent as utf-8 instead of Unicode?

Unicode has several variations, of which UTF-8 is the most popular. So I’m not sure I understand your question.

Xojo can convert encodings, so if you have text in UTF16 and you wanted to convert it to UTF8 before sending, you would do this:

// suppose data16 contains a string in UTF16 encoding
// convert to UTF8
dim data as string = data16.ConvertEncoding(Encodings.UTF8)

See ConvertEncoding — Xojo documentation

2 Likes

No, Unicode is just a list of different characters, each of which is assigned its own unicode number. How these are then stored in memory or on disk is what the encoding is, UTF-8 being one such. Data sent via a TCPSocket is just bytes. It’s up to the sender and receiver to agree on what the encoding is, if any, of all or parts of each packet sent/received.

The copright symbol is as follows:

U+00A9 © c2 a9 COPYRIGHT SIGN

So it has unicode number hex A9, and in UTF-8 the character is stored as C2 A9. In UTF-16 it will still be unicode A9, but the bytes representing it in UTF-16 will be different.

Sorry I asked the wrong question. What I meant is URL encoded characters.
For example sending </02/> from xojo is received as %3c%2f032%2f%3e.
If I send the same line from say firefox it is received as </02/>.
I apologize for my ignorance on this subject, thanks for your patience.

Can you post the code you are using to send the data?

Every non-ascii character is converted to URL encoded. I have a way to parse it and fix it on the micro controller. It just seems like I should be able to control the way it is sent.and save an additional process.

</101/><start>
[{"hx":"75","hn":"60","hd":"10","cx":"0","cn":"0","cd":"0","al":0,"st":1,"hw":0,"de":"1"},
{"hx":"60","hn":"50","hd":"5","cx":"0","cn":"0","cd":"0","al":0,"st":0,"hw":1,"de":"4"},
{"hx":"160","hn":"110","hd":"20","cx":"0","cn":"0","cd":"0","al":0,"st":1,"hw":0,"de":"3"},
{"hx":"0","hn":"0","hd":"0","cx":"0","cn":"0","cd":"0","al":0,"st":0,"hw":0,"de":"0"}]
<end>

TCPSocket1.Address = GB1.ip(ndx)
TCPSocket1.Port = GB1.pt(ndx)
TCPSocket1.Connect

While Not TCPSocket1.IsConnected
  ' poll the socket to let it do its thing
  TCPSocket1.Poll
  ' if an error occurs, the Error event will fire
Wend

If TCPSocket1.IsConnected Then
  ' proceed using the connection
  TCPSocket1.Write(mdlID + pStart + json + pEnd)
Else
  MessageBox("Connection failed!")
End If

But how are you attempting to send this? Please post that code.

TCPSocket1.Address = GB1.ip(ndx)
TCPSocket1.Port = GB1.pt(ndx)
TCPSocket1.Connect

While Not TCPSocket1.IsConnected
  ' poll the socket to let it do its thing
  TCPSocket1.Poll
  ' if an error occurs, the Error event will fire
Wend

If TCPSocket1.IsConnected Then
  ' proceed using the connection
  TCPSocket1.Write(mdlID + pStart + json + pEnd)
Else
  MessageBox("Connection failed!")
End If

I’ve never seen Xojo URL encode data sent on a socket.

I suggest you look at the variables mdlID, pStart, json & pEnd in the debugger as the URL encoded data is more than likely already in one of them.

1 Like

To expand on that, TCPSocket sends only the bytes you give it. Set a breakpoint at the .Write and check those variables.

This is what is being presented to the write.
but now I am having an issue with connecting.

I need to look at this some more, thanks

Var ss as String = mdlID + pStart + json + pEnd

Contents of ss

</121/><start>
[{"hx":75,"hn":60,"hd":10,"cx":0,"cn":0,"cd":0,"al":0,"st":1,"hw":0,"de":1},{"hx":60,"hn":50,"hd":5,"cx":0,"cn":0,"cd":0,"al":0,"st":0,"hw":1,"de":4},{"hx":160,"hn":110,"hd":20,"cx":0,"cn":0,"cd":0,"al":0,"st":1,"hw":0,"de":3},{"hx":0,"hn":0,"hd":0,"cx":0,"cn":0,"cd":0,"al":0,"st":0,"hw":0,"de":0}]<end>

I changed a few too many things at once. It is all working like it should now.

Thanks for all of the help.

2 Likes