Salesforce

How Do I Convert a Binary BLOB with UTF-8 Content to a Unicode BLOB? (Magic xpi 4.5)

« Go Back

Information

 
Created ByKnowledge Migration User
Approval Process StatusPublished
Objective
Description

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:

  1. 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.

  2. To create the variables that you will use in this example, open the Flow Variables repository.

  3. There, create two BLOB variables called F.utf8 and F.utf16. Then, create a logical variable called F.log.

  4. 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.

  5. Drag a Flow Data utility into your flow.

  6. 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.

  7. In the Action column, select Update from the drop-down list.

  8. In the Type column, select Flow.

  9. In the Name column, select the F.utf8 BLOB variable that you previously created.

  10. In the Encoding column, because there is no native representation for UTF-8, select Binary.

  11. In the Update Expression column, click to open the Expression Editor.

  12. 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.

  1. In the Flow Data Configuration dialog box, click Add.

  2. In the Action column, select Update from the drop-down list.

  3. In the Type column, select Flow.

  4. In the Name column, select the F.utf16 BLOB variable that you previously created.

  5. In the Encoding column, select Unicode.

  6. In the Update Expression column, click to open the Expression Editor.

  7. 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.

  1. In the Flow Data Configuration dialog box, click Add.

  2. In the Action column, select Update from the drop-down list.

  3. In the Type column, select Flow.

  4. In the Name column, select the F.log logical variable that you previously created.

  5. In the Update Expression column, click to open the Expression Editor.

  6. 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).

  1. Click OK to close the Flow Data Configuration dialog box.

  2. Build your project.

  3. Open the Magic xpi Monitor, and start the project.

  4. 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.

  1. Stand on the second line in the Flow Data Configuration dialog box, and click Delete.

  2. Click Add to create a new line.

  3. In the Action column, select Update from the drop-down list.

  4. In the Type column, select Flow.

  5. In the Name column, select the F.utf16 BLOB variable that you previously created.

  6. In the Encoding column, select Unicode.

  7. In the Update Expression column, click to open the Expression Editor.

  8. In the Expression Editor, enter the following expression (as shown in the image below), and then click OK:

    UTF8toUnicode ( F.utf8 )

  9. 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.

  10. In what is now the third entry in the Flow Data Configuration dialog box, go to the Update Expression column and click .

  11. 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).

  1. Click OK to close the Flow Data Configuration dialog box.

  2. Build your project.

  3. Open the Magic xpi Monitor, and start the project.

  4. 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.

Related Topics

How Do I Convert a Unicode BLOB to a Binary BLOB with UTF-8 Content?

Reference
Attachment 
Attachment