I would like to parse a binary block of data into the component parts. In C/C++, I could easily copy the block to memory, and address it using a struct, and then the fields would be populated based on their offset into the binary block.

I haven't found a way to do something like this in SalesForce, but I have considered using the EncodingUtil to convert it into Base64, then pulling off and manipulating the number of bytes I need, and casting to the type.

Does anyone have another solution, or any other thoughts on the subject?

1 Answer 1


Apex doesn't have mechanisms (by design?) for manipulating binary data. You may be able to get your Base64 approach to work but it is likely to be pretty ugly code. (See e.g. this).

If you have a lot of different binary data to process, I suggest that instead you write the parsing in your favourite language and host a HTTPS web service somewhere (e.g. Heroku). Pass the binary data to the web service using the Apex Http classes and return say JSON that can be converted into Apex objects using the classes generated by e.g. json2apex. The web service can be stateless and so simple. There is no need for authentication: the service holds no information and its unpublished API is probably enough to stop anyone else ever discovering or using it. Talking to it only over HTTPS keeps the communication content private. I have used this pattern in one case where I needed to access a 3rd party Java library to verify some data and it is working fine for me.

(If your processing can be kicked off in the browser then you could also consider Processing Binary Protocols with Client-Side JavaScript).

  • +1 Heroku got a lot less attractive since they introduced sleeping dynos for free purposes, in my opinion. Not to mention, callouts are only effective in certain contexts. I completely agree that using something like this design is practical, perhaps even necessary, for larger payloads.
    – sfdcfox
    Oct 24, 2015 at 5:20

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.