Sample code for 30+ languages & platforms
PureBasic Requires Chilkat v11.7.0+

QR Code Numeric, Alphanumeric and Byte Modes

See more QR Code Examples

Demonstrates the QR encoding modes. Digits alone are stored in numeric mode, and text made only of digits, upper-case letters, space and $ % * + - . / : in alphanumeric mode. Both pack more characters into a symbol than byte mode, which is used for everything else. QrCode chooses the most compact mode the text allows, and the response reports it. A product code written in upper case therefore gives a smaller symbol than the same code in lower case.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkFormats.pb"
IncludeFile "CkJsonObject.pb"

Procedure ChilkatExample()

    success.i = 0

    ;  This example requires the Chilkat API to have been previously unlocked.
    ;  See Global Unlock Sample for sample code.

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

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

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

    ;  Digits only.
    CkJsonObject::ckUpdateString(json,"payload.text","00123456789012345678")
    success = CkFormats::ckQrCode(fmt,"generate",json,jsonOut)
    If success = 0
        Debug CkFormats::ckLastErrorText(fmt)
        CkFormats::ckDispose(fmt)
        CkJsonObject::ckDispose(json)
        CkJsonObject::ckDispose(jsonOut)
        ProcedureReturn
    EndIf

    Debug "digits: mode " + CkJsonObject::ckStringOf(jsonOut,"symbols[0].selectedEncoding.mode") + ", version " + Str(CkJsonObject::ckIntOf(jsonOut,"symbols[0].selectedEncoding.version"))

    ;  Upper-case letters, digits, space and - are all in the alphanumeric set.
    CkJsonObject::ckUpdateString(json,"payload.text","ORDER-12345 LOT A7 SHELF 12-C")
    success = CkFormats::ckQrCode(fmt,"generate",json,jsonOut)
    If success = 0
        Debug CkFormats::ckLastErrorText(fmt)
        CkFormats::ckDispose(fmt)
        CkJsonObject::ckDispose(json)
        CkJsonObject::ckDispose(jsonOut)
        ProcedureReturn
    EndIf

    Debug "upper case: mode " + CkJsonObject::ckStringOf(jsonOut,"symbols[0].selectedEncoding.mode") + ", version " + Str(CkJsonObject::ckIntOf(jsonOut,"symbols[0].selectedEncoding.version"))

    ;  Lower-case letters are not, so the same text in lower case needs byte mode.
    CkJsonObject::ckUpdateString(json,"payload.text","order-12345 lot a7 shelf 12-c")
    success = CkFormats::ckQrCode(fmt,"generate",json,jsonOut)
    If success = 0
        Debug CkFormats::ckLastErrorText(fmt)
        CkFormats::ckDispose(fmt)
        CkJsonObject::ckDispose(json)
        CkJsonObject::ckDispose(jsonOut)
        ProcedureReturn
    EndIf

    Debug "lower case: mode " + CkJsonObject::ckStringOf(jsonOut,"symbols[0].selectedEncoding.mode") + ", version " + Str(CkJsonObject::ckIntOf(jsonOut,"symbols[0].selectedEncoding.version"))

    ;  The mode can be named.  Byte mode can hold any text.  Naming a mode the text cannot use
    ;  (numeric for text with letters, for example) fails with conflictingOptions.
    CkJsonObject::ckUpdateString(json,"payload.text","00123456789012345678")
    CkJsonObject::ckUpdateString(json,"encoding.qr.mode","byte")
    success = CkFormats::ckQrCode(fmt,"generate",json,jsonOut)
    If success = 0
        Debug CkFormats::ckLastErrorText(fmt)
        CkFormats::ckDispose(fmt)
        CkJsonObject::ckDispose(json)
        CkJsonObject::ckDispose(jsonOut)
        ProcedureReturn
    EndIf

    Debug "digits as bytes: mode " + CkJsonObject::ckStringOf(jsonOut,"symbols[0].selectedEncoding.mode") + ", version " + Str(CkJsonObject::ckIntOf(jsonOut,"symbols[0].selectedEncoding.version"))

    ;  The four symbols, in the order they were made:

    ;  image

    ;  Sample output:

    ;  digits: mode numeric, version 1
    ;  upper case: mode alphanumeric, version 2
    ;  lower case: mode byte, version 3
    ;  digits as bytes: mode byte, version 2


    CkFormats::ckDispose(fmt)
    CkJsonObject::ckDispose(json)
    CkJsonObject::ckDispose(jsonOut)


    ProcedureReturn
EndProcedure