URL Encoding/Decoding Tool

Frequently Asked Questions

What is URL Encoding?

URL encoding is a method of converting characters into a format that can be transmitted over the Internet. URLs can only be sent over the Internet using the ASCII character set. Since URLs often contain characters outside the ASCII set, they need to be converted. URL encoding replaces unsafe ASCII characters with a '%' followed by two hexadecimal digits.

How Does URL Encoding Work?

URL encoding works by replacing unsafe characters with a '%' followed by two hexadecimal digits representing the UTF-8 encoding of that character. For example, the space character is encoded as %20. URLs cannot contain spaces, so they are replaced with either a plus sign (+) or %20. Other special characters are replaced with their corresponding %xx codes.

Common Characters That Need URL Encoding

The following are common characters that need URL encoding when used in URLs:

CharacterURL EncodedDescription
Space%20Most common character that requires URL encoding
!%21Exclamation mark
"%22Double quote
#%23Hash symbol (used for URL fragments)
$%24Dollar sign
%%25Percent sign (URL encoding escape character)
&%26Ampersand (used for URL parameter separation)
'%27Single quote
(%28Opening parenthesis
)%29Closing parenthesis
*%2AAsterisk
+%2BPlus sign
,%2CComma
/%2FForward slash (URL path separator)
:%3AColon
;%3BSemicolon
=%3DEquals sign (URL parameter assignment)
?%3FQuestion mark (URL query string start)
@%40At symbol
[%5BOpening square bracket
\%5CBackslash
]%5DClosing square bracket
^%5ECaret
{%7BOpening curly brace
|%7CVertical bar
}%7DClosing curly brace
~%7ETilde

URL Encoding Implementation in Different Programming Languages

Here are examples of URL encoding and decoding in various programming languages:

Go

package main

import (
    "fmt"
    "net/url"
)

func main() {
    // Encode a URL
    text := "Hello World! Special chars: &?=/";
    encoded := url.QueryEscape(text)
    fmt.Println("Encoded:", encoded)

    // Decode a URL
    decoded, err := url.QueryUnescape(encoded)
    if err == nil {
        fmt.Println("Decoded:", decoded)
    }
}

C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

// Function to URL-encode a string
char *url_encode(char *str) {
    char *encoded = malloc(strlen(str) * 3 + 1);
    char *pstr = str;
    char *pbuf = encoded;

    while (*pstr) {
        if (isalnum(*pstr) || *pstr == '-' || *pstr == '_' || *pstr == '.' || *pstr == '~') {
            *pbuf++ = *pstr;
        } else if (*pstr == ' ') {
            *pbuf++ = '+';
        } else {
            sprintf(pbuf, "%%%.2X", *pstr);
            pbuf += 3;
        }
        pstr++;
    }
    *pbuf = '\\0';

    return encoded;
}

int main() {
    char *text = "Hello World! Special chars: &?=/";
    char *encoded = url_encode(text);

    printf("Original: %s\\n", text);
    printf("Encoded: %s\\n", encoded);

    free(encoded);
    return 0;
}

PHP

<?php
// URL encoding
$text = "Hello World! Special chars: &?=/";
$encoded = urlencode($text);
echo "Encoded: " . $encoded . "\\n";

// URL decoding
$decoded = urldecode($encoded);
echo "Decoded: " . $decoded . "\\n";
?>

Python

import urllib.parse

# URL encoding
text = "Hello World! Special chars: &?=/"
encoded = urllib.parse.quote(text)
print(f"Encoded: {encoded}")

# URL decoding
decoded = urllib.parse.unquote(encoded)
print(f"Decoded: {decoded}")

JavaScript

// URL encoding
const text = "Hello World! Special chars: &?=/";
const encoded = encodeURIComponent(text);
console.log("Encoded:", encoded);

// URL decoding
const decoded = decodeURIComponent(encoded);
console.log("Decoded:", decoded);

TypeScript

// URL encoding
const text: string = "Hello World! Special chars: &?=/";
const encoded: string = encodeURIComponent(text);
console.log("Encoded:", encoded);

// URL decoding
const decoded: string = decodeURIComponent(encoded);
console.log("Decoded:", decoded);

What's the Difference Between encodeURI and encodeURIComponent?

encodeURI() is designed to encode a complete URI, so it does not encode characters that have special meaning in a URL, such as /, ?, :, \u0040, \u0026, =, +, $, and #. encodeURIComponent(), on the other hand, encodes every character that has special meaning, making it ideal for encoding individual query string parameters. When encoding URL parameters, always use encodeURIComponent() to ensure all special characters are properly encoded.