URL Encode, Decode function for MFC

CK CK 3788 
Created at
Updated at  
605 0 0

Here's the good example for URL Encoding & Decoding.
 

inline BYTE toHex(const BYTE &x)
{
    return x > 9 ? x + 55: x + 48;
}

inline BYTE toByte(const BYTE &x)
{
    return x > 57? x - 55: x - 48;
}

CString URLDecode(CString sIn)
{
    CString sOut;
    const int nLen = sIn.GetLength() + 1;
    register LPBYTE pOutTmp = NULL;
    LPBYTE pOutBuf = NULL;
    register LPBYTE pInTmp = NULL;
    LPBYTE pInBuf =(LPBYTE)sIn.GetBuffer(nLen);
    //alloc out buffer
    pOutBuf = (LPBYTE)sOut.GetBuffer(nLen);
    
    if(pOutBuf)
    {
        pInTmp   = pInBuf;
        pOutTmp = pOutBuf;
        // do encoding
        while (*pInTmp)
        {
            if('%'==*pInTmp)
            {
                pInTmp++;
                *pOutTmp++ = (toByte(*pInTmp)%16<<4) + toByte(*(pInTmp+1))%16;
                pInTmp++;
            }
            else if('+'==*pInTmp)
                *pOutTmp++ = ' ';
            else
                *pOutTmp++ = *pInTmp;
            pInTmp++;
        }
        *pOutTmp = '';
        sOut.ReleaseBuffer();
    }
    sIn.ReleaseBuffer();
    
    return sOut;
}

CString URLEncode(CString sIn)
{
    CString sOut;
    const int nLen = sIn.GetLength() + 1;
    register LPBYTE pOutTmp = NULL;
    LPBYTE pOutBuf = NULL;
    register LPBYTE pInTmp = NULL;
    LPBYTE pInBuf =(LPBYTE)sIn.GetBuffer(nLen);
    //alloc out buffer
    pOutBuf = (LPBYTE)sOut.GetBuffer(nLen*3);
    
    if(pOutBuf)
    {
        pInTmp   = pInBuf;
        pOutTmp = pOutBuf;
        // do encoding
        while (*pInTmp)
        {
            if(isalnum(*pInTmp) || '-'==*pInTmp || '_'==*pInTmp || '.'==*pInTmp)
                *pOutTmp++ = *pInTmp;
            else if(isspace(*pInTmp))
                *pOutTmp++ = '+';
            else
            {
                *pOutTmp++ = '%';
                *pOutTmp++ = toHex(*pInTmp>>4);
                *pOutTmp++ = toHex(*pInTmp%16);
            }
            pInTmp++;
        }
        *pOutTmp = '';
        sOut.ReleaseBuffer();
    }
    sIn.ReleaseBuffer();
    
    return sOut;
}

 

Above sample code use CString, so if you want to use it on another type of development environment such as gcc or something like that, you need to add some more stuffs because it may not support CString class.
 

Tags C++ Image Resizing formatted date Facebook X
Comments 0
Similar posts
  1. How to load HTML resource on MFC ?
    841
  2. Makefile to automate the build process for C++
    279
  3. How to call SetTimer function on MFC CDialog class ?
    650
  4. How to search file on certain directory ?
    629
  5. KMP String Matching Algorithm
    672
  6. Is there URLEncode function in Javascript ?
    393
  7. How to run shell command by MFC ?
    435
  8. Binary Search Sample Code
    405
  9. Double Linked List based in C++
    796
  10. MFC based World Wide Web HTTP Server Source Code
    459
  11. Creating and Using a CAsyncSocket Object to use CAsyncSocket
    472
  12. Fast and Good Keyboard/Mouse Test without the message handler
    982
  13. Simple CGI Programming in C
    545
  14. C++ CGI Example working on Apache
    484
  1. Getting Started with FAISS: A CentOS 7 Installation Guide
    280
  2. CMake - a cross-platform build system generator that creates Makefiles used in C++ projects
    295
  3. How To Check If Your Printer Is Connected Using C#
    1,026
  4. KMP String Matching Algorithm
    672
  5. How to get process id in C# like windows task manager ?
    870
  6. How to implement switch statement in C# ?
    987
  7. How to implement while loop in C# ?
    1,341
  8. How to delete file ?
    1,192
  9. How to convert integer to text string ?
    697
  10. How to convert text string to integer ?
    610
  11. How to set an certain color as transparent color on bitmap
    512
  12. How to resize and save PNG or Jpeg Image file ?
    631
  13. How to display message box in C#
    1,109
  14. How to convert bitmap image to jpeg image ?
    356
  15. To load image from file
    450
  16. How to load HTML resource on MFC ?
    841
  17. How to get GM Time as String ?
    422
  18. Makefile to automate the build process for C++
    279
  19. Finding process name on OS
    407
  20. Convert System.DateTime to UNIX timestamp
    381
  21. Retrieve system information
    495
  22. How to call SetTimer function on MFC CDialog class ?
    650
  23. How to rename(change file name) in C# ?
    1,075
  24. Find Files in certain directory
    926
  25. Implementing C#'s foreach loop in Delphi 8
    459
  26. How to detect resource leaks in a Form
    939
  27. The best way to enable and disable buttons, menu items and toolbar buttons
    667
  28. How to exit from a console application
    810
  29. How to restrict a program to a single instance
    729
  30. Compress and decompress strings in C#
    971
Recently updated
  1. Why 100% SEO Guarantees Better User Engagement and How to Achieve It in Your Service
    40
  2. Why SPF and DKIM Are Essential for Linux Email Servers?
    34
  3. How do I create animated background for Google Meet?
    20,611
  4. MaxMind GeoLite2 vs. DB-IP Lite vs. ip-api.com: Choosing the Right Free IP Geolocation Solution
    244
  5. Meet Valkey: Why Redis Changed Its License, Why Valkey is Better, and How to Use It as a Drop-in Replacement
    94
  6. How Rogue Web Scrapers Are Stealing My Content and Destroying My Server
    157
  7. Array of arguments passed to script in PHP
    90
  8. Invalid Auction Storms in Ad Tech
    98
  9. Building MRC-Compliant Instrumentation in Ad Tech: Implementation Steps and Key Challenges
    202
  10. The Two Clocks of Ad Server Architecture
    70
  11. Frequency Cap Architecture Overview
    514
  12. Collecting Proof-of-Play from 100,000 Screens
    146
  13. Vector Search in Relational Databases (RDBs): Trends & Usage Environments
    287
  14. What is RedisVL?
    109
  15. Designing a Skill Registry Architecture: Minimize Token Usage and Maximize AI Performance
    230
  16. TPU & GPU Cluster Management Tools
    243
  17. The Concept of Scaffolding in AI Agents
    233
  18. Beyond Static Pipelines: Architecting LLM Multi-Agent Systems for Autonomous Workflow Orchestration
    583
  19. AI-DLC vs. AI-SDLC: Model-Centric vs. System-Centric AI Development
    258
  20. Fine-Tuning Open-Weight LLMs: Training, Licensing, and Deploying Custom Models with Ollama
    339
  21. Inside the Next-Generation Streaming Architecture: Ultra-Low Latency, SGAI, and AI Codecs
    202
  22. Machine Learning: Training vs. Ranking
    221
  23. The Continuous Learning Machine: Designing a Loop That Actually Turns
    222
  24. The Next-Gen Low-Latency Standards: LL-HLS & MoQ
    195
  25. Designing a Data Curation System Around a Vector Database
    220
  26. Extracting Context from Live Video: The Architecture Got Simpler When We Stopped Building Features
    256
  27. The New Ad Stack Behind Live Sports Streaming: Inside SGAI, EABN, and the Standards Race to Fix Live Monetization
    656
  28. Designing MLOps Architectures to Handle Seasonal Data Drift in EV Fleet Models
    195
  29. From 15 to 40+ FPS: Accelerating PyTorch Object Detection on Automotive ECUs
    217
  30. Understanding Data Lineage
    260