I have recently stumbled upon a rather interesting problem on my end. I have set up an integration with external system, and it responds me with a Base64 encoded Hex code of a PDF document whenever I make a call with a single input string. I'm supposed to obtain the Hex code after Base64 decoding, then cast the hex code into a blob & save it as an attachment body.

The problem here is: The Hex code itself and its length does not match when I debug. The string is one character less from the size, and the conversion from hex to blob fails because of odd numbered size of input string - it turns out that the size is correct here.

Below is the code:

//Proforma response = Base64 encoded hex string
Blob afterblob = EncodingUtil.base64Decode(proformaResponse);
string HexCode = afterblob.toString();
Debug.Proforma2__c = HexCode;
Debug.Debug1__c = string.valueOf(Hexcode.length());
update Debug;

Note that I use a custom debug object because of sandbox environment's incapability on debugging webservice calls...

When I make the call, HexCode string length is 52312 using some tool to calculate it, and Debug1 field value is 52313. Later I submit the HexCode to the EncodingUtil class' ConvertFromHex function, but it fails while saying "input string char count is an odd number"... When I discarded the endmost string of HexCode, it worked, but that's not an approach I want to adopt.

I appreciate any clarification on this issue.


When the decoded Base64 string appears to be blank, the variable's size is calculated as 1. Here is the screensho: (Proforma1:Base64//Proforma2:Decoded into hex//Debug1:Proforma2's length)

enter image description here

  • What tool are you using to calculate the size? Also, what do you mean by this? "Note that I use a custom debug object because of sandbox environment's incapability on debugging webservice calls..." System.debug logging has always served me well enough. – Mark Pond Mar 28 '16 at 22:19
  • I've used several online tools (as simple as charactercount.com) and MS Word to calculate the character size. I imported external system's WSDL into Salesforce and whenever I make calls via execute anonymous window, absolutely no debugs are displayed, even CALLOUT_RESPONSE appears blank. It's a known limitation, so I consulted that workaround. – TSunar Mar 29 '16 at 5:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.