Home SALESFORCEAPEX How to convert from String to Blob using Apex Class?

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 = ‘TheBlogReaders.com’;
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.

Salesforce.com 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 = ‘TheBlogReaders.com’;
// 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());

You may also like

Leave a Comment