I’m having some questions on the new framework.
It all starts with string data from a (old style) TCPSocket.
The dataavailable event gives me a string with a nil encoding. I need that string to be placed inside Xojo.Core.MemoryBlock.
How am i suppose to do this? I’ve tried some ways but it didn’t seem to work as i thought.
Dim Input As String = Me.ReadAll(Nil)
Dim mb As Xojo.Core.MemoryBlock = Xojo.Core.TextEncoding.ASCII.ConvertTextToData( Input, True ) // True to alowlossy?
// Here i'll be splitting the memoryblock (mb) into packets that worked before with fake data (nil encoding using ASCII to convert).
Basicly the “Input” is a string made from bytes (hex or Byte 0-255). I want to adapt this to the new framework so we can move on and make it more future proof.
Based on your responses above, I’ve put together this method for sanitising data I retrieve from an SQLite database, which may be UTF8 (most of it certainly is), but where there may be non UTF8 mixed with it:
[code]function sanitiseUTF8 (instr as string) as text
// Converts the input string instr (assumed to be intended to be UTF8) to Text,
// but at the same time ensures that any bad UTF8 is replaced with the Unicode
// replacement character (U+FFFD)
dim mbc as MemoryBlock = instr, mb as new Xojo.Core.MemoryBlock (mbc, mbc.Size)
return Xojo.Core.TextEncoding.UTF8.ConvertDataToText (mb, true)
end function
[/code]
That this may copy the data three times seems a bit overkill to me. Probably the strings are short so no great loss of time, but it all seems a bit clumsy.
Thanks for that - one or two useful extra hints there. I’ve kept mine as I want to show the bad chars, but I’ve optimised it a bit to avoid doing much when the string is clean.
Any idea why a string can’t just be cast to a memoryblock? I keep seeing hints in the doccy that they are both the same.