UTF8 to Bytes

The following code is VB.NET:

Dim rawData = System.Text.Encoding.UTF8.GetBytes(myString)

I think this encodes all the characters in the specified string into a sequence of bytes.

What would be a similar solution in Xojo ?

Check out how to use the memoryblock: Page Not Found — Xojo documentation :

[code]dim m as memBlockString

//create a new memBlockString
m = new MemBlockString

//add the strings into the memBlockString
m.appendString textField1.text
m.appendString textField2.text

//retreive the string
Label1.text = m.getString[/code]

Thanks! :slight_smile:

Any idea why we need to “say” twice, that m is a memBlockString ?

dim m as memBlockString m = new MemBlockString

That’s old code: [quote]dim m as new MemoryBlock[/quote] is fine.

So would the following translate my initial question code ?

Dim rawData As MemoryBlock = myString

I think theres something missing to convert to UTF 8 first …

[quote=113533:@w storm]o would the following translate my initial question code ?
Dim rawData As MemoryBlock = myString[/quote]


See http://documentation.xojo.com/index.php/MemoryBlock where example are shown. For instance :

Dim s As String = "abcde" Dim mb As MemoryBlock mb = s MsgBox(Str(mb.Size)) //displays 5

you can use ConvertEncoding(myString, encodings.UTF8) first to make sure you get UTF8 bytes.

Ok, I would end up with something like this:

Dim rawData As MemoryBlock = ConvertEncoding(myString, Encodings.UTF8)

Also, how would you convert an integer in to a Byte datatype ?


frame(0) = CByte(129)

integer to byte? Just assign it.

So this:

frame(0) = CByte(129)

and this

Dim frame As New MemoryBlock(0) frame.byte(0)=129
is the same ?

frame(0) would access first entry in an array of memory blocks, so that’s not what you want.

Dim frame As New MemoryBlock(1) frame.Byte(0)=129 frame.Byte(0)=ctype(129, Byte)

The thing I wanted to tell is that the ctype() operation is not needed here. But both lines work.

Sorry, I missed the VB.NET first part which is as follows:

Dim frame(10) As Byte frame(0) = CByte(129) frame(1) = CByte(... and so on)

So in Xojo it would become

Dim frame As New MemoryBlock(10)
  frame.Byte(0)=ctype(129, Byte)

What you’re saying is that VB.NET frame(0), the 0 is an array of bytes and in Xojo the frame is a 10 bytes block where the first byte (0) is INT 129, is that correct ?

you can also declare array of byte in Xojo:

Dim frame(10) As Byte frame(0) = 129 frame(1) = otherVlaue
This also works.

Memoryblock is often used for API things.
CType() calls are often not needed.

You can also get bytes out of a UTF8 string with

[quote]dim b as byte
dim s As string = “Get A Value”
b = s.midB(5,1).ascB
which will result in b = 65