In converting my existing application to Xojo, one small part of what I am doing is this. I have some data in a Text variable which is base64-encoded. To go with that, I have an encoding to apply to the decoded data. The difficulty is that the encoding may be wrong or the decoded data may contain some characters that cannot be decoded with the supplied encoding. I want, however, to keep as much good data as possible.
After reading a lot of the documentation, I discovered today that the TextEncoding.ConvertDataToText method allows for lossy conversion. So I’ve put together this small example:
Dim myblk as Xojo.core.MemoryBlock, tmptxt As MemoryBlock, encoding as Xojo.Core.TextEncoding
Dim i as Integer, charset, intxt, fintxt as Text, mybytes() as byte
charset = "ASCII" // I'm told the data is ASCII (it's actually UTF-8)
intxt = "SGVyZSBpcyBzb21lIHTDqXh0" // This is the base-64 encoded data
tmptxt = DecodeBase64 (intxt) // Base64-decode data into a classic memory-block
for i=0 to tmptxt.Size-1
mybytes.append (tmptxt.Byte(i)) // Copy the classic memory-block into an array of bytes
next
try
encoding = xojo.Core.TextEncoding.FromIANAName (charset)
Exception
encoding = xojo.Core.TextEncoding.FromIANAName ("Windows-1252")
end try
myblk = new xojo.Core.MemoryBlock (mybytes) // Make a xojo.core.memoryblock from the array of bytes (another copy)
fintxt = encoding.ConvertDataToText (myblk, true) // Finally convert to Text, allowing bad characters to be dropped
msgbox (fintxt)
To effect this, I have to go through several copying steps and wondered if what I have here can be improved upon.