This topic contains 4 replies, has 3 voices, and was last updated by  Ryan McBee 9 years, 10 months ago.

Decoding Base64 In HL7

  • I’m writing my first interface for handling orders and I find myself having to decode a field containing a Base64 encryption of a PDF. filter.base64.dec() is decoding the information and writing a valid PDF file, and contains part of the data visible in the full PDF. But it’s not the complete PDF.

    Can anyone advise what I might be doing wrong? I’d bet it has something to do with how I’m handling the string, I’m not very experianced in that. I’m at a loss as to what it is and would appreciate any help anyone can provide.

    function main(MSG)
       DestinationPath = 'c:/test/'
    function MSGParser(MSG)
       rawmsg = hl7.parse{vmd='Image.vmd',data=MSG}
       file = rawmsg.Group4[1].Group5.Group7[1].OBX[5][5]
       filetype = rawmsg.Group4[1].Group5.Group7[1].OBX[5][3]
       PatMrnNO = rawmsg.Group1.PID[3][1]
       PatAcctNo = rawmsg.Group1.PID[18][1]
       HospitalSited = rawmsg.MSH[4][1]   
    function CreateImage()
       local FileName ='Trace_'..PatMrnNO..'_'..PatAcctNo..'_%Y%m%d%H%M%S.'..filetype)
       local F =, "w")

    M.R. McBee

    try to change mode in which you open the file (PDF files they binary) and don’t call to string() since content of OBX-5 is anyway string:

    local F =, “wb”)


    When assigning the value to ‘file’, try this:

    file = rawmsg.Group4[1].Group5.Group7[1].OBX[5][5]:nodeValue()

    This will ensure you get the raw value. Calling tostring() on the HL7 node will perform HL7 escaping on the value, which can get you into trouble in this context. So when writing to the file, omit the call to tostring() (since you used :nodeValue() above):


    That should hopefully do the trick!

    yes, Kevin is right, I missed the nodeValue() call, having said that, notice the file mode ‘wb’.

    Thanks a bunch guys. Works like a charm.

    M.R. McBee


You must be logged in to reply to this topic.