How to convert a String into a new xojo.core.MemoryBlock?

Hopefully helpful:

  Dim theStr As String
  theStr = "a" // some data in the String

  Dim mb As MemoryBlock // old memory block
  mb = theStr // this is no copy, isn't it?
  
  Dim p As Ptr // type Ptr exists in the classic and new framework
  p = mb
  
  Dim xmb As xojo.Core.MutableMemoryBlock
  xmb = new xojo.Core.MutableMemoryBlock(p, theStr.LenB)

Be carefull:
As far as I know from this forum, xmb isn’t a copy of the data within the String!
Think of it as a wrapper holding a pointer, size and other stuff from the data within the String object.

Has anybody checked, whether mb = theStr and new xojo.Core.MutableMemoryBlock(p, theStr.LenB) doesn’t do a copy?
Thanx
Jrg

http://developer.xojo.com/xojo-core-textencoding$ConvertTextToData
http://developer.xojo.com/xojo-core-mutablememoryblock

Dim theStr As String theStr = "a" // some data in the String dim xmb as new Xojo.Core.MutableMemoryBlock(Xojo.Core.TextEncoding.UTF8.ConvertTextToData(theStr.toText))

[quote=311000:@Joerg Friedrich]Hopefully helpful:

  Dim theStr As String
  theStr = "aä" // some data in the String

  Dim mb As MemoryBlock // old memory block
  mb = theStr // this is no copy, isn't it?
  
  Dim p As Ptr // type Ptr exists in the classic and new framework
  p = mb
  
  Dim xmb As xojo.Core.MutableMemoryBlock
  xmb = new xojo.Core.MutableMemoryBlock(p, theStr.LenB)

Be carefull:
As far as I know from this forum, xmb isn’t a copy of the data within the String!
Think of it as a wrapper holding a pointer, size and other stuff from the data within the String object.

Has anybody checked, whether mb = theStr and new xojo.Core.MutableMemoryBlock(p, theStr.LenB) doesn’t do a copy?
Thanx
Jörg[/quote]

The code above creates a new Xojo.Core.MutableMemoryBlock.

[quote=311057:@Michel Bujardet]http://developer.xojo.com/xojo-core-textencoding$ConvertTextToData
http://developer.xojo.com/xojo-core-mutablememoryblock

Dim theStr As String theStr = "aä" // some data in the String dim xmb as new Xojo.Core.MutableMemoryBlock(Xojo.Core.TextEncoding.UTF8.ConvertTextToData(theStr.toText))[/quote]
An what if the String does contain binary data (non-utf8)?

It’s a copy.

instead of theStr.LenB, I would use mb.size here.

You cannot shove binary into the Text format. That is the whole point. String is a bucket of bytes, essentially. Text is only representing text. If you try what I posted with binary data, you will get an exception.

Why not affect the string to a classic memoryBlock and use it as constructor for the MutableMemoryBlock.

Well, I had a String …
I haven’t checked it out but the MemoryBlock the CTOR of MutableMemoryBlock expects, is of type xojo.core.MemoryBlock and not the classical one.

Then you can probably try binary stream, or Ptr. Both exist in both frameworks.