How to convert from String to Blob using Apex Class?

From Blob to String:-

blob tempBlob = report.getContent();
String tempString = tempBlob.toString();

From String to Blob:-

String tempString = ‘’;
Blob beforeblob = Blob.valueOf(tempString);

Base64 is often used when you need to encode binary data into characters and its a good way of taking binary data and turning it into text so that it can easily be transmitted like html data to Email. makes it pretty easy to perform Base-64 encoding in Apex via their EncodingUtil class

Below is an Sample Apex code with a very simple example of the base-64 encode/decode.

string tempString = ‘’;
// convert from string to blob
Blob tempBlob = Blob.valueOf(tempString);

// convert base64 encode from the blob
string paramvalue = EncodingUtil.base64Encode(tempBlob);

// print base64 encode value
System.debug(before + ‘ is now encoded as: ‘ + paramvalue);

// convert base64 decode from the after the base64 encode value
Blob tempBlob = EncodingUtil.base64Decode(paramvalue);

// print base64 decode value
System.debug(paramvalue + ‘is now decoded as: ‘ + tempBlob.toString());

Written by 

3x Salesforce MVP | Platform Champion | 18x Salesforce Certified | MuleSoft Certified | 8x Trailhead Ranger | TechForce Services | Sydney | Australia

Leave a Reply

Your email address will not be published.