Sample code for 30+ languages & platforms
PureBasic

Convert any File to Base64 (and back)

Demonstrates how to get the contents of any file as a base64 string, and then write it back.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkBinData.pb"

Procedure ChilkatExample()

    success.i = 0

    bd.i = CkBinData::ckCreate()
    If bd.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ; This example will load a PDF and return it as a base64 string.
    success = CkBinData::ckLoadFile(bd,"qa_data/pdf/helloWorld.pdf")
    If success <> 1
        Debug "Failed to load file."
        CkBinData::ckDispose(bd)
        ProcedureReturn
    EndIf

    b64Str.s = CkBinData::ckGetEncoded(bd,"base64")
    Debug b64Str

    ; Now write the base64 string back to the binary PDF file:
    bd2.i = CkBinData::ckCreate()
    If bd2.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkBinData::ckAppendEncoded(bd2,b64Str,"base64")
    success = CkBinData::ckWriteFile(bd2,"qa_output/helloWorld.pdf")


    CkBinData::ckDispose(bd)
    CkBinData::ckDispose(bd2)


    ProcedureReturn
EndProcedure