I am working on an iOS project and having some problems with TCP networking. In a desktop app, I would do something like this:
tcpsocket.write chr(233)+chr(0)
but I haven’t had any luck getting figuring out how to do this in iOS builds. I’ve tried numerous ways of creating a memory block with the text values, including trying the Text.FromUnicodeCodepoint, but nothing has worked so far.
Can anyone help with an iOS equivalent to:
tcpsocket.write chr(233)+chr(0)
Update - I can make this work for values 0-127, but not for 128 to 255. I need to be able to send ASCII characters 128 to 255.
&u233 (for example) will cause a runtime exception if I try to send in ASCII encoding. But if I use UTF8, I get 2 bytes on the receiving end. So, with a &u223, I get 195 and 159 on the receiving end (when sending as UTF8).
[quote=154253:@Jon Daggett]Update - I can make this work for values 0-127, but not for 128 to 255. I need to be able to send ASCII characters 128 to 255.
&u233 (for example) will cause a runtime exception if I try to send in ASCII encoding. But if I use UTF8, I get 2 bytes on the receiving end. So, with a &u223, I get 195 and 159 on the receiving end (when sending as UTF8).
Anyone have any ideas for me?[/quote]
Can’t you get the data on the other end in UTF-8 ? That would be the simplest solution by far.
Yes I could, however this is working with several other existing apps that have been around for over 10 years. I need to stay compatible.
I’ve done this in RB/RS/Xojo for years by using the CHR command and values of 0 to 255 with no problem. Also did it in an iOS app in X-code back in 2008 as well (except of course not with a CHR command). So, I’m sure it’s possible, but haven’t found the right approach.
Basically I need to send a string of raw values (0 to 255 range) as single bytes. So, UTF-8 won’t work, yet ASCII throws a runtime error.
[quote=154256:@Jon Daggett]Yes I could, however this is working with several other existing apps that have been around for over 10 years. I need to stay compatible.
I’ve done this in RB/RS/Xojo for years by using the CHR command and values of 0 to 255 with no problem. Also did it in an iOS app in X-code back in 2008 as well (except of course not with a CHR command). So, I’m sure it’s possible, but haven’t found the right approach.
Basically I need to send a string of raw values (0 to 255 range) as single bytes. So, UTF-8 won’t work, yet ASCII throws a runtime error.[/quote]
Without seeing your code, I believe the problem here comes from using a unicode point. You mention code from 10 years ago, and there lies the problem. At the time, strings were frequently also bytes chunks, and “higher ASCII” was in fact a byte. Unicode points in UTF-8 are a whole different thing.
In the past, for instance, ASCII(233) was e acute é (Latin-1). Byte of hex value A9.
A Unicode point has a byte order mark, so é in Unicode takes two bytes : C3 + A9.
So what you want to insert in your data memoryblock is not &u233, but b as byte = 233.
Well, the 10 year old code really isn’t the problem. I’m still using that in stuff compiled with recent versions of RS & Xojo. I’m just needing to send values 0-255 as a byte in a TCP packet as a series of single bytes.
But how do I do that? It was CHR(223) for hex DF (or 0xDF in X-code), but I can’t seem to get that into the memoryblock. Even if I use the character, it gets to the receiver as 2 bytes, not one byte.
If I use:
dim m as xojo.Core.MemoryBlock = xojo.Core.TextEncoding.UTF8.ConvertTextToData(t)
with “t” being the text data, I get two bytes.
If I use:
dim m as xojo.Core.MemoryBlock = xojo.Core.TextEncoding.ASCII.ConvertTextToData(t)
then I get a runtime exception (since it apparently doesn’t like any bytes with a value higher than 127).
I’ve tried putting the text in as:
dim t as text = &uDF
and also as:
dim t as text = “”
But still comes through as 2 bytes on the receive end.
There is no such thing as “high ascii” and the new framework has a clear distinction between text and binary data. What you are wanting to send are precise bytes/binary data (not text!)- so I think you might be making it harder than it needs to be. I haven’t really tested this, but I think you want something like:
Dim myBytes() As Byte
myBytes.Append(233)
myBytes.Append(0)
Dim mb As MemoryBlock = New MemoryBlock(myBytes)