MemoryBlock confusion

Can someone explain why this give 2 different strings in the TextAreas please.
Encrypted() As Byte. This Ubound is 31 and the first 4 elements are:128, 152, 201, 135

Dim L As Integer = Ubound(Encrypted) Dim mb As MemoryBlock Dim sTemp As String For c As Integer = 0 to L Dim num As Integer = Encrypted(c) sTemp = sTemp + Encodings.UTF16.Chr(num) mb(c) = Encrypted(c) Next TextArea1.Text = sTemp //Also tried DefineEncoding(sTemp, Encodings.UTF16) TextArea2.Text = mb //DefineEncoding(mb, Encodings.UTF16)

TextArea1.Text with sTemp
???3
t w ?j,??o`?

TextArea2 with mb
ɇ͟3
t w j,o`

TextArea2 with DefineEncoding(mb, Encodings.UTF16)
???

Love to but that code doesn’t compile or run so …

This does and gives the same result for each

  dim encrypted() as integer = array(128, 152, 201, 135)
  
  Dim L As Integer = Ubound(Encrypted)
  Dim mb As new MemoryBlock(8)
  Dim sTemp As String
  For c As Integer = 0 to L
    Dim num As Integer = Encrypted(c)
    sTemp = sTemp + Encodings.UTF16.Chr(num)
    mb.Int16Value(c*2) = Encrypted(c)
  Next
  TextArea1.Text = sTemp //Also tried DefineEncoding(sTemp, Encodings.UTF16) 
  TextArea2.Text = DefineEncoding(mb,  Encodings.UTF16)

Norman beat me to it. mb is never instantiated and then is used later as if it were an array, and then not even in a way that would work.

Oops on the code I forgot the .Byte

my code has mb.Byte©

well there’s the problem
one is using UTF16 (which is 2 bytes per char) and one is using BYTE which is one byte per char

the code I posted should be something you can adapt to your real code

Thanks, yes I have it showing both the same now. So if I have L as the Length of the Encrypted then

mb = New MemoryBlock (L*2) ?

So the next question would be when I read that back into a MemoryBlock why is the MemoryBlock size 48?

Dim mb As MemoryBlock = DefineEncoding(TextArea1.Text, Encodings.UTF16)

now mb.Size = 48.

[quote=295434:@Jym Morton]Thanks, yes I have it showing both the same now. So if I have L as the Length of the Encrypted then

mb = New MemoryBlock (L*2) ?
[/quote]
Seems about right if encoded is an array of bytes

[quote=295434:@Jym Morton]So the next question would be when I read that back into a MemoryBlock why is the MemoryBlock size 48?

Dim mb As MemoryBlock = DefineEncoding(TextArea1.Text, Encodings.UTF16)

now mb.Size = 48.[/quote]

Why are you using define encoding when reading it from the text area ?
That makes no sense
You MIGHT use CONVERT encoding - text areas dont display raw unencoded bytes - the data is ALWAYS in some well known encoding

Ahhh, that brought it back to Size = 64.
Thank you

UTF-16 encodes characters as either two bytes or four bytes. You might be better off using an array rather than a MemoryBlock here.

I don’t know what you want to achieve eventually, but you may want to investigate the new framework MemoryBlock and MutableMemoryBlock, as well as the new Text data type.

None of the issues you encounter would be possible with these.

Thanks Michel, I just made a class to encrypt\decrypt using the AES algorithm. (for learning) it works fine as long as I stay with UTF8. I was trying to make it UTF16 but I’m giving up :slight_smile:

Precisely, the new framework manages that much better.