Salesforce

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

« Go Back

Information

 
Created ByKnowledge Migration User
Approval Process StatusPublished
Objective
Description

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

Magic xpi lets you easily convert a Unicode BLOB to a binary BLOB with UTF-8 content. You do this with the UTF8fromUnicode function.

This topic makes use of the utf16_output_valid.txt Unicode BLOB that was created in the How Do I Convert a Binary BLOB with UTF-8 Content to a Unicode BLOB? topic. If you create a new project for this current topic, you should copy the utf16_output_valid.txt file over to the new project’s <My Documents>\Magic\projects\ folder.

If you did not previously work with the How Do I Convert a Binary BLOB with UTF-8 Content to a Unicode BLOB? topic, you should create your own Unicode BLOB file to work with this example and save it under your <My Documents>\Magic\projects\<current project> folder. Call this file utf16_output1.txt.

Converting a Unicode BLOB to a binary BLOB with UTF-8 content using the UTF8fromUnicode function:

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

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

  3. Drag a Flow Data utility into your flow.

  4. To begin the conversion process, you need to load the Unicode BLOB into a file using the File2Blb function. In the Flow Data Configuration dialog box, click Add.

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

  6. In the Type column, select Flow.

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

  8. In the Encoding column, select Unicode.

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

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

    File2Blb (EnvVal ('currentprojectdir')&'utf16_output1.txt')

The next stage is to convert the utf16_output1.txt file to a UTF-8 BLOB variable using the UTF8fromUnicode 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.utf8 BLOB variable that you previously created.

  5. In the Encoding column, select Binary.

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

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

    UTF8fromUnicode ( F.utf16 )

Next, you need to write the UTF-8 BLOB variable to the file system.

  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.utf8 , EnvVal('currentprojectdir') &'utf8_output.txt')

Now, run the project to see what is created in the utf8_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 utf8_output.txt file.

When you open the utf8_output.txt file, its contents are correctly encoded in UTF-8. If you check the contents using a Hex Editor, you will see that the file does not contain a BOM.

The next stage explains how to add a BOM to BLOB variables.

  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.utf8 BLOB variable.

  5. In the Encoding column, select Binary.

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

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

    BlobAddBOM ( F.utf8 ,1)

  8. Use the up arrow on the right hand side of the Flow Data Configuration dialog box to move this new entry up to the third line of the table.

  9. Delete the existing utf8_output.txt file from the <My Documents>\Magic\projects\<current project> folder.

Now, run the project again to see what is created this time in the utf8_output.txt file.

  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 updated utf8_output.txt file.

When you open the utf8_output.txt file, its contents are correctly encoded in UTF-8. Not only that, if you check the contents using a Hex Editor, you will see that the file also contains a BOM.

Related Topics

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

Reference
Attachment 
Attachment