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 

I am Sakthivel Madesh, a certified Salesforce Advanced Developer & Administrator working on Salesforce Technology since 2011. I am currently working in Techforce Services as Sr. Salesforce Developer. I have worked on multiple technologies Like PHP, Java, Salesforce, I am very passionate about Salesforce and to earn 15x Salesforce Certification. I love Trailhead for learning Salesforce Skill and Become a Trailhead 5x Ranger too. I Love to Learn & Share my Salesforce knowledge to Salesforce Learners/Beginner using My Blog and Salesforce Community.

Leave a Reply

Your email address will not be published. Required fields are marked *