1

I've a scenario where i'm saving the file as content version is attching to a record when i use blob.valueOf() it's failing and when i use EncodingUtil.base64Decode() the method is creating record . I thought i'll check the difference but the documentation doesn't provide anything

1
  • 1
    In a similar way to how all Natural Numbers are Integers (but not all Integers are Natural Numbers)... All Base64-encoded strings can be represented as a BLOB, but not all BLOBs represent a Base64-encoded string. Please edit your question to show us the code that is failing and copy/paste the error message that you're getting.
    – Derek F
    Commented Jun 19 at 13:24

1 Answer 1

5

tl;dr: Blob.valueOf doesn't encode or decode the string, but uses it as is, while EncodingUtil.base64Encode and EncodingUtil.base64Decode convert to/from a 7-bit safe encoding format for use in transmitting across protocols (SMTP, etc) or formats (JSON, XML, etc) that cannot directly handle binary data.


Blob.valueOf converts a String directly into a Blob, without any encoding or decoding.

Blob b = Blob.valueOf('Hello World');
String s = b.toString();
System.debug(s); // Hello World

This can be useful when you need a Blob version of a String. For example, you can use this method when you want to save a plain text file into a ContentVersion, since VersionData must always be a Blob.

Conversely, EncodingUtil.base64Decode takes a Base 64-encoded string and converts it to binary data.

Blob b = EncodingUtil.base64Decode('SGVsbG8gV29ybGQ=');
String s = b.toString();
System.debug(s); // Hello World

In Base 64, a binary string is converted into a "safe" 7-bit character string by breaking up 3 8-bit binary data values into 4 7-bit character values, such that 64 printable characters are treated as digits in a number.

As a simple example, consider a three-byte value written as 0x01 0x02 0x03. These are unprintable characters, but we want to be able to have them be available as plain text, perhaps because we're using a system that can only understand ASCII (7-bit characters).

To get there, we first write out the data in binary:

00000001 00000010 00000011

We then group the bits into six bit groups.

000000 010000 001000 000011

Now, we use each group as an index into the following string:

ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/

The values in our six bit grouping are the following indexes:

0 16 8 3

Where 000000 is A, and 111111 is /. Converting these indexes to characters, we get the encoded value AQID for 0x010203.

A special character = indicates that there weren't 3 bytes left at the end. If there were two bytes, the last character is =, and if there was only 1 byte, the last two characters will be ==.

EncodingUtil.base64Encode implements the algorithm I described above, and EncodingUtil.base64Decode reverses the process, converting each character into a six bit number, then reorganizing the bits into 8-bit bytes.

The contents of the Blob might not be able to be converted to a String, which is always a UTF-8 encoded String in Apex.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .