How to convert String to Base64 using Salesforce Apex?

How to convert String to Base64 using Salesforce Apex?

EncodingUtil.base64Decode() can be used to convert Base64 content to Blob value. Then, we can use toString() to convert the Blob value to String.

Sample Code:

String base64Content = EncodingUtil.Base64Encode( 
    Blob.valueOf( 'Testing' ) 
);
System.debug( 'Base64 Content is ' + base64Content );
Blob blobContent = EncodingUtil.base64Decode( base64Content );
System.debug( 'Decoded Base64 value is ' + blobContent.toString() );

Output:

Leave a Reply