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)