---
schema_version: "1"
id: 082f42d6b4873b90ec6e8680dbae1e527cebdc97aa67966ecf42673ef1582cda
title: Encoding Your Data
collection: legacydeveloper
version: current
locale: en-us
portal_url: https://docs.salesforce.com/legacydeveloper/current/en-us/apexcode/apex_encoding.md
canonical_url: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_encoding.htm
source_site: developer.salesforce.com
content_type: documentation
content_hash: 64b7c18a8f80eee16771f3c2898e5509dc346e3e03489cf794c9fde3348b8c45
description: You can encode and decode URLs and convert strings to hexadecimal format by using the methods provided by the EncodingUtil class.
---
# 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.

DateTime d = System.now();
String timestamp = ''\+ d.year() + '-' +
    d.month() + '-' +
    d.day() + '\\'T\\'' +
    d.hour() + ':' +
    d.minute() + ':' +
    d.second() + '.' +
    d.millisecond() + '\\'Z\\'';
System.debug(timestamp);
String urlEncodedTimestamp = EncodingUtil.urlEncode(timestamp, 'UTF-8');
System.debug(urlEncodedTimestamp);

This next example shows how to use convertToHex to compute a client response for HTTP Digest Authentication (RFC2617). In this example, myData is a placeholder value.

@IsTest
private with sharing class SampleTest {
    @IsTest
    static void testConvertToHex() {
        String myData = 'A Test String';
        Blob hash = Crypto.generateDigest('SHA1',Blob.valueOf(myData));
        String hexDigest = EncodingUtil.convertToHex(hash);
        System.debug(hexDigest);  
    } 
}

#### See Also

- [_Apex Reference Guide_: EncodingUtil Class](https://developer.salesforce.com/docs/atlas.en-us.264.0.apexref.meta/apexref/apex_classes_restful_encodingUtil.htm "Apex Reference Guide: EncodingUtil Class - HTML (New Window)")