Sample code for 30+ languages & platforms
C++

URL Encoding and Decoding

See more Encryption Examples

Demonstrates URL encoding and decoding.

Chilkat C++ Downloads

C++
#include <CkStringBuilder.h>

void ChilkatSample(void)
    {
    bool success = false;

    //  To URL encoding a string:
    const char *s = "Why a > b?";

    CkStringBuilder sb;
    success = sb.Append(s);

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

    //  Show the URL encoded string:
    const char *sEncoded = sb.getAsString();
    std::cout << sEncoded << "\r\n";

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

    //  If you prefer "+" instead of "%20" for SPACE chars:
    int numReplaced = sb.Replace("%20","+");
    std::cout << sb.getAsString() << "\r\n";

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

    //  To decode:
    sb.Decode("url","utf-8");
    std::cout << sb.getAsString() << "\r\n";

    //  Result is: Why a > b?
    }