How Do I Convert a Binary BLOB with UTF-8 Content to a Unicode BLOB? (Magic xpi 4.5)
To convert a binary BLOB with UTF-8 content to a Unicode BLOB, you need to use the UTF8toUnicode function.
Converting a binary BLOB with UTF-8 content to a Unicode BLOB using the UTF8toUnicode function:
-
For the purpose of this example, begin by creating a binary BLOB file. Save it in UTF-8 encoding under your <My Documents>\Magic\projects\<current project> folder. Call this file utf8.txt.
-
To create the variables that you will use in this example, open the Flow Variables repository.
-
There, create two BLOB variables called F.utf8 and F.utf16. Then, create a logical variable called F.log.
-
Now, create the variables that you will use in this example. Open the Flow Variables repository and create two BLOB variables called F.utf8 and F.utf16. Then, create a logical variable called F.log under the same tab.
-
Drag a Flow Data utility into your flow.
-
To begin the conversion process, you need to load the binary BLOB file from the file system using the File2Blb function. In the Flow Data Configuration dialog box, click Add.
-
In the Action column, select Update from the drop-down list.
-
In the Type column, select Flow.
-
In the Name column, select the F.utf8 BLOB variable that you previously created.
-
In the Encoding column, because there is no native representation for UTF-8, select Binary.
-
In the Update Expression column, click to open the Expression Editor.
-
In the Expression Editor, enter the following expression (as shown in the image below), and then click OK:
File2Blb (EnvVal ('currentprojectdir')&'utf8.txt')
By way of illustration, you can see what will happen if you try to convert the utf8.txt file to Unicode in a simple way, without using the UTF8toUnicode function. This will result in a corrupted file because of the lack of UTF-8 support.
-
In the Flow Data Configuration dialog box, click Add.
-
In the Action column, select Update from the drop-down list.
-
In the Type column, select Flow.
-
In the Name column, select the F.utf16 BLOB variable that you previously created.
-
In the Encoding column, select Unicode.
-
In the Update Expression column, click to open the Expression Editor.
-
To update the F.utf16 BLOB variable with the contents of the F.utf8 BLOB variable, enter the following expression in the Expression Editor (as shown in the image below), and then click OK:
F.utf8
The next stage is to save the F.utf16 BLOB variable to the file system as a file named utf16_output. You do this with the Blb2File function.
-
In the Flow Data Configuration dialog box, click Add.
-
In the Action column, select Update from the drop-down list.
-
In the Type column, select Flow.
-
In the Name column, select the F.log logical variable that you previously created.
-
In the Update Expression column, click to open the Expression Editor.
-
In the Expression Editor, enter the following expression (as shown in the image below), and then click OK:
Blb2File ( F.utf16 , EnvVal('currentprojectdir') &'utf16_output.txt')
Next, run the project to see exactly what is created in the utf16_output.txt file that you defined in step 6 (above).
-
Click OK to close the Flow Data Configuration dialog box.
-
Build your project.
-
Open the Magic xpi Monitor, and start the project.
-
Open the <My Documents>\Magic\projects\<current project> folder. You will see the newly-created utf16_output.txt file.
When you open the utf16_output.txt file, note that its contents are corrupted.
Now, you should make the following changes to convert the UTF-8 file to Unicode, using the UTF8toUnicode function. This will ensure that you receive correctly encoded contents.
-
Stand on the second line in the Flow Data Configuration dialog box, and click Delete.
-
Click Add to create a new line.
-
In the Action column, select Update from the drop-down list.
-
In the Type column, select Flow.
-
In the Name column, select the F.utf16 BLOB variable that you previously created.
-
In the Encoding column, select Unicode.
-
In the Update Expression column, click to open the Expression Editor.
-
In the Expression Editor, enter the following expression (as shown in the image below), and then click OK:
UTF8toUnicode ( F.utf8 )
-
Use the up arrow on the right hand side of the Flow Data Configuration dialog box to move this new entry up to the second line of the table.
-
In what is now the third entry in the Flow Data Configuration dialog box, go to the Update Expression column and click .
-
Change the expression to:
Blb2File ( F.utf16 , EnvVal('currentprojectdir') &'utf16_output_valid.txt')
Now, run the project again to see what is created in the utf16_output_valid.txt file that you defined in step 11 (above).
-
Click OK to close the Flow Data Configuration dialog box.
-
Build your project.
-
Open the Magic xpi Monitor, and start the project.
-
Open the <My Documents>\Magic\projects\<current project> folder. You will see the newly-created utf16_output_valid.txt file.
When you open the utf16_output_valid.txt file, you will see that its contents are correctly encoded. This is because you used the UTF8toUnicode function to convert the UTF-8 content into Unicode.
How Do I Convert a Unicode BLOB to a Binary BLOB with UTF-8 Content?