memory block to text : The data could not be converted to text with this encoding

hello, i’m updating some code from string to text, and i have many problems with encodings and converting to text.

like i take this example from the doc, i just added the last 2 lines to display the result, can’t figure out why


Sub Action()
Using Xojo.Core
Using Xojo.Crypto

Dim salt,wTxt As Text = “SaltValue”
Dim saltMB As MemoryBlock
saltMB = Xojo.Core.TextEncoding.UTF8.ConvertTextToData(salt)

Dim password As Text = “YourPasswordSentence”
Dim passwordMB As MemoryBlock
passwordMB = Xojo.Core.TextEncoding.UTF8.ConvertTextToData(password)

Dim hash As MemoryBlock
hash = PBKDF2(saltMB, passwordMB, 100, 32, HashAlgorithms.SHA512)

crash here : wTxt = Xojo.Core.TextEncoding.UTF8.ConvertDataToText(hash)
The data could not be converted to text with this encoding.
i tried ascii encoding the same

TextField1.Text = wTxt


with tcp packet too i have some issues from memory block to text.

like message : Expected class MemoryBlock, but got class Xojo.Core.MemoryBlock
if i replace MemoryBlock by Xojo.Core.MemoryBlock in the current example so i a bit confused, how to convert hash to totext in this example…

thanks a lot

You don’t, because it’s not. The hash is an arbitrary series of bytes, not UTF-8 encoded at all, and Text is not a substitute for String. Text holds characters as if it were an array of Unicode code points, whereas String holds a series of bytes. Those bytes can have no encoding, making them a kind of read-only MemoryBlock, or they could have some encoding that tells Xojo how to convert those bytes into human-readable characters. Nothing says that the assigned encoding has to be valid and it’s up to you to make sure it is.

To convert your hash to Text, you’d have to encode it into some ASCII representation first like hex or Base64. Neither of those functions are native to the new framework yet but I’ve included them in my M_Text module.

(BTW, in the future please use the [code] tags when posting code to make it easier on us.)