EncodingUtil Class

Use the methods in the EncodingUtil class to encode and decode URL strings, and convert strings to hexadecimal format.

System

You cannot use the EncodingUtil methods to move documents with non-ASCII characters to Salesforce. You can, however, download a document from Salesforce. To do so, query the ID of the document using the API query call, then request it by ID.

The following are methods for EncodingUtil. All methods are static.

Converts a Base64-encoded String to a Blob representing its normal form.

public static Blob base64Decode(String inputString)

Type: Blob

Converts a Blob to an unencoded String representing its normal form.

public static String base64Encode(Blob inputBlob)

  • inputBlob: Type: Blob

Type: String

Converts the specified hexadecimal (base 16) string to a Blob value and returns this Blob value.

public static Blob convertFromHex(String inputString)

  • inputString: Type: String The hexadecimal string to convert. The string can contain only valid hexadecimal characters (0-9, a-f, A-F) and must have an even number of characters.

Type: Blob

Each byte in the Blob is constructed from two hexadecimal characters in the input string.

The convertFromHex method throws the following exceptions.

  • NullPointerException — the inputString is null.
  • InvalidParameterValueException — the inputString contains invalid hexadecimal characters or doesn’t contain an even number of characters.

Returns a hexadecimal (base 16) representation of the inputBlob. This method can be used to compute the client response (for example, HA1 or HA2) for HTTP Digest Authentication (RFC2617).

public static String convertToHex(Blob inputBlob)

  • inputBlob: Type: Blob

Type: String

Decodes a string in application/x-www-form-urlencoded format using a specific encoding scheme, for example “UTF-8.”

public static String urlDecode(String inputString, String encodingScheme)

Type: String

This method uses the supplied encoding scheme to determine which characters are represented by any consecutive sequence of the form \"%xy\". For more information about the format, see The form-urlencoded Media Type in Hypertext Markup Language - 2.0.

Encodes a string into the application/x-www-form-urlencoded format using a specific encoding scheme, for example “UTF-8.”

public static String urlEncode(String inputString, String encodingScheme)

Type: String

The rules that apply when the method encodes a string:

  • These characters remain the same:

    • Alphanumeric characters A - Z, a - z, and 0 -9.
    • Special characters dot (.), hyphen (-), asterisk (*), and under score (_).
  • The space character is converted to a plus sign (+).

  • All other characters are unsafe. This method uses the supplied encoding scheme to obtain the bytes for unsafe characters. For more information about the format, see The form-urlencoded Media Type in Hypertext Markup Language - 2.0.

Here, url is a placeholder value.