Since setting up my new business Wickedbit I have been spending a lot of time looking at integrating Salesforce with various third party applications. I love integration work which is just as well as one is never like another, despite there being standards for things like authentication and data payloads people still seem to relish the chance to “roll their own”. I shouldn’t complain I guess it keeps me in a job!

One such “example of innovation” that I came across required me to pass a blob via HTTP and to achieve this rather than simply Base64 encoding the Blob I was required to send the Blob as a string of hexadecimal values. An interesting idea for sure and one that after a quick bit of searching I realised wasn’t straight forward to solve.

My solution to the problem has ended up being a little convoluted and I’m sure there are other ways around this but for prosperities sake here is my approach. I decided to use the platform EncodingUtil class to get the Blob into a base64 representation; then using some simple bit shifting and masking I would reverse the base64 encoding and therefore get the individual bytes that make up the blob. From there it’s just a case of doing a decimal to hex conversion. It sounds easy enough and here’s the code; it’s compressed into as few statements as possible to try and keep below governor limits.

This could all be made a lot simpler if there were two little additions to the platform, other than the inclusion of a byte primitive. Firstly access to the bytes that make up the blob. Either let me initialise a list of bytes using the blob or provide a getByteAt(n) method. Secondly, a simple decimal to hex conversion method would clean this up no end and be useful in other circumstances too.

Whilst you might not have an odd integration requirement like I did to fulfil I hope that the idea above will be of some use; if you remove the lookup into the hex map you have a routine that can give you the list of bytes that make up a blob – that’s got to be useful to someone, no? I hope so.