String data to Xojo.Core.MemoryBlock

Hi innovators;

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.

The String is really acting like a “bucket of bytes” so you can go through the classic MemoryBlock to convert it.

dim mbc as MemoryBlock = Input
dim mb as new Xojo.Core.MemoryBlock( mbc, mbc.Size )

Thanks Kem!

That should work for me.

[quote=245116:@Kem Tekinay]The String is really acting like a “bucket of bytes” so you can go through the classic MemoryBlock to convert it.

dim mbc as MemoryBlock = Input dim mb as new Xojo.Core.MemoryBlock( mbc, mbc.Size ) [/quote]

How many times is this copying the data?

Twice, I think, but that’s easy to test.

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.

FYI, my M_String module has a function to “clean” a UTF-8 string.

http://www.mactechnologies.com/index.php?page=downloads#m_string

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.

Strings are not mutable. MemoryBlocks are, so the bytes have to be copied.