EncodingUtil Class

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

Namespace

System

Usage

Note
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 APIquery call, then request it by ID.

EncodingUtil Methods

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

base64Decode(String)

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

Signature

public static Blob base64Decode(String inputString)

Parameters

inputString
Type: String

Return Value

Type: Blob

base64Encode(Blob)

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

Signature

public static String base64Encode(Blob inputBlob)

Parameters

inputBlob
Type: Blob

Return Value

Type: String

convertFromHex(String)

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

Signature

public static Blob convertToHex(String inputString)

Parameters

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.

Return Value

Type: Blob

Usage

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.

Example

Blob blobValue = EncodingUtil.convertFromHex('4A4B4C');
System.assertEquals('JKL', blobValue.toString());

convertToHex(Blob)

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

Signature

public static String convertToHex(Blob inputString)

Parameters

inputString
Type: Blob

Return Value

Type: String

urlDecode(String, String)

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

Signature

public static String urlDecode(String inputString, String encodingScheme)

Parameters

inputString
Type: String
encodingScheme
Type: String

Return Value

Type: String

Usage

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

urlEncode(String, String)

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

Signature

public static String urlEncode(String inputString, String encodingScheme)

Parameters

inputString
Type: String
encodingScheme
Type: String

Return Value

Type: String

Usage

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.

Example

String encoded = EncodingUtil.urlEncode(url, 'UTF-8');
© Copyright 2000–2014 salesforce.com, inc. All rights reserved.
Various trademarks held by their respective owners.