Sample code for 30+ languages & platforms
Unicode C

URL Encoding and Decoding

See more Encryption Examples

Demonstrates URL encoding and decoding.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkStringBuilderW.h>

void ChilkatSample(void)
    {
    BOOL success;
    const wchar_t *s;
    HCkStringBuilderW sb;
    const wchar_t *sEncoded;
    int numReplaced;

    success = FALSE;

    // To URL encoding a string:
    s = L"Why a > b?";

    sb = CkStringBuilderW_Create();
    success = CkStringBuilderW_Append(sb,s);

    // URL encode the string.
    CkStringBuilderW_Encode(sb,L"url",L"utf-8");

    // Show the URL encoded string:
    sEncoded = CkStringBuilderW_getAsString(sb);
    wprintf(L"%s\n",sEncoded);

    // The result is:  Why%20a%20%3E%20b%3F

    // If you prefer "+" instead of "%20" for SPACE chars:
    numReplaced = CkStringBuilderW_Replace(sb,L"%20",L"+");
    wprintf(L"%s\n",CkStringBuilderW_getAsString(sb));

    // Output is:   Why+a+%3E+b%3F

    // To decode:
    CkStringBuilderW_Decode(sb,L"url",L"utf-8");
    wprintf(L"%s\n",CkStringBuilderW_getAsString(sb));

    // Result is: Why a > b?


    CkStringBuilderW_Dispose(sb);

    }