Note: This release is in preview. Features described here don’t become generally available until the latest general availability date that Salesforce announces for this release. Before then, and where features are noted as beta, pilot, or developer preview, we can’t guarantee general availability within any particular time frame or at all. Make your purchase decisions only on the basis of generally available products and features.

Encoding Your Data

You can encode and decode URLs and convert strings to hexadecimal format by using the methods provided by the EncodingUtil class.

This example shows how to URL encode a timestamp value in UTF-8 by calling urlEncode.

1DateTime d = System.now();
2String timestamp = ''+ d.year() + '-' +
3    d.month() + '-' +
4    d.day() + '\'T\'' +
5    d.hour() + ':' +
6    d.minute() + ':' +
7    d.second() + '.' +
8    d.millisecond() + '\'Z\'';
9System.debug(timestamp);
10String urlEncodedTimestamp = EncodingUtil.urlEncode(timestamp, 'UTF-8');
11System.debug(urlEncodedTimestamp);

This next example shows how to use convertToHex to compute a client response for HTTP Digest Authentication (RFC2617).

1@isTest
2private class SampleTest {
3   static testmethod void testConvertToHex() {
4      String myData = 'A Test String';
5      Blob hash = Crypto.generateDigest('SHA1',Blob.valueOf(myData));
6      String hexDigest = EncodingUtil.convertToHex(hash);
7      System.debug(hexDigest);  
8    } 
9}