Sample code for 30+ languages & platforms
PureBasic

Append Encoded Binary Data to StringBuilder

Demonstrates how to append encoded binary data to the contenets of a StringBuilder.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkBinData.pb"
IncludeFile "CkStringBuilder.pb"

Procedure ChilkatExample()

    success.i = 0

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

    success = CkBinData::ckLoadFile(bd,"qa_data/jpg/starfish.jpg")
    If success = 0
        Debug "Failed to load file."
        CkBinData::ckDispose(bd)
        ProcedureReturn
    EndIf

    ; For example, let's say we want construct simple JSON containing the base64 representation of the above JPG file.
    sb.i = CkStringBuilder::ckCreate()
    If sb.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkStringBuilder::ckAppend(sb,"{ " + Chr(34) + "jpg" + Chr(34) + ": " + Chr(34))
    ; GetEncodedSb appends the enocded representation of the binary data to the StringBuiler passed in the 2nd arg.
    CkBinData::ckGetEncodedSb(bd,"base64",sb)
    CkStringBuilder::ckAppend(sb,Chr(34) + " }")

    Debug CkStringBuilder::ckGetAsString(sb)

    ; Output looks like this:
    ;  { "jpg": "/9j/4AAQSkZJRgABAg...rcQ+vo//2Q==" }


    CkBinData::ckDispose(bd)
    CkStringBuilder::ckDispose(sb)


    ProcedureReturn
EndProcedure